Android自定義view實現(xiàn)輸入框效果
更新時間:2021年03月15日 08:15:31 作者:天真的趙日天
這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)輸入框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android自定義view實現(xiàn)輸入框的具體代碼,供大家參考,具體內(nèi)容如下
自定義輸入框的View
package com.fenghongzhang.day017; import android.content.Context; import android.content.res.TypedArray; import android.text.InputType; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; public class InputView extends LinearLayout { private int inputview_input_icon; private String inputview_input_hint; private boolean inputview_is_pass; private View inflate; ImageView imageView; EditText editText; public InputView(@NonNull Context context) { super(context); } public InputView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context,attrs); } public void init(Context context,AttributeSet attr){ if(attr==null){ return; } TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.InputView); inputview_input_icon = typedArray.getResourceId(R.styleable.InputView_input_icon, R.mipmap.ic_launcher); inputview_input_hint = typedArray.getString(R.styleable.InputView_input_hint); inputview_is_pass = typedArray.getBoolean(R.styleable.InputView_is_pass, false); //釋放資源 typedArray.recycle(); //加載. inflate = LayoutInflater.from(context).inflate(R.layout.inputview, this, false); imageView= (ImageView)inflate.findViewById(R.id.icon); editText= (EditText)inflate.findViewById(R.id.text); imageView.setImageResource(inputview_input_icon); editText.setText(inputview_input_hint); //設(shè)置樣式,是不是密文格式,可以沒有. editText.setInputType(inputview_is_pass? InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD:InputType.TYPE_CLASS_PHONE); //添加到viewgroup中 addView(inflate); } //用來取到輸入框的值. public String getString(){ return editText.getText().toString().trim(); } }
輸入框的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:orientation="horizontal" android:layout_height="50dp" android:background="@drawable/back_color" android:layout_gravity="center_vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_gravity="center_vertical" android:id="@+id/icon" /> <EditText android:layout_marginLeft="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:hint="username" android:textSize="30dp" android:background="@null" android:id="@+id/text" /> </LinearLayout>
屬性文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="InputView"> <!--圖片--> <attr name="input_icon" format="reference"></attr> <!--字體--> <attr name="input_hint" format="string"></attr> <!--是否密文--> <attr name="is_pass" format="boolean"></attr> </declare-styleable> </resources>
布局中引用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main3Activity"> <com.fenghongzhang.day017.InputView android:layout_width="300dp" android:layout_height="50dp" my:input_icon="@mipmap/ic_launcher" my:input_hint="手機號" my:is_pass="true" > </com.fenghongzhang.day017.InputView> </LinearLayout>
輸入框圓角背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" ></corners> <size android:width="100dp" android:height="30dp"></size> <solid android:color="@color/colorAccent"></solid> </shape>
效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android動畫 實現(xiàn)開關(guān)按鈕動畫(屬性動畫之平移動畫)實例代碼
這篇文章主要介紹了Android動畫 實現(xiàn)開關(guān)按鈕動畫(屬性動畫之平移動畫)實例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android中應(yīng)用界面主題Theme使用方法和頁面定時跳轉(zhuǎn)應(yīng)用
在Android SDK中內(nèi)置了下面的Theme,可以按標題欄Title Bar和狀態(tài)欄Status Bar是否可見來分類,感興趣的朋友可以了解下哈2013-06-06Android中Market的Loading效果實現(xiàn)方法
這篇文章主要介紹了Android中Market的Loading效果實現(xiàn)方法,較為詳細的分析了Android中l(wèi)oading效果的相關(guān)布局及功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10android側(cè)滑菜單控件DrawerLayout使用方法詳解
這篇文章主要為大家詳細介紹了android側(cè)滑菜單控件DrawerLayout的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android開發(fā)實現(xiàn)帶有反彈效果仿IOS反彈scrollview教程詳解
本文給大家分享android開發(fā)實現(xiàn)帶有反彈效果,模仿ios反彈scrollview詳細教程,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09