Android自定義EditText實(shí)現(xiàn)淘寶登錄功能
本文主要是自定義了EditText,當(dāng)EditText有文本輸入的時(shí)候會(huì)出現(xiàn)刪除圖標(biāo),點(diǎn)擊刪除圖標(biāo)實(shí)現(xiàn)文本的清空,其次對(duì)密碼的返回做了處理,用*替代系統(tǒng)提供的.。
首先看效果圖:

整體布局UI:
<com.example.zdyedittext.ClearEditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:background="@android:color/white"
android:ems="10"
android:hint="手機(jī)號(hào)"
android:padding="8dp"
android:singleLine="true" />
<com.example.zdyedittext.ClearEditText
android:id="@+id/et_pass_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼"
android:background="@android:color/white"
android:password="true"
android:padding="8dp"
android:singleLine="true" />
自定義EditText類(lèi)
由于自定義EditText理所當(dāng)然要集成EditText
public class ClearEditText extends EditText
然后添加構(gòu)造方法,是為了能在XML中能夠引用。
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
接下來(lái)就是設(shè)置自己的EditText的樣式,添加自己想要的樣式。具體是在init()方法中實(shí)現(xiàn)。
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
init()方法的實(shí)現(xiàn)過(guò)程:[2]參數(shù)為:dr.mDrawableRight,定義刪除按鈕是在EditText的右邊,設(shè)置圖標(biāo)的左上右下:mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
private void init() {
// 獲取EditText的DrawableRight,假如沒(méi)有設(shè)置我們就使用默認(rèn)的圖片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = getResources().getDrawable(R.drawable.del);//R.drawable.del刪除圖標(biāo)的圖片
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
//設(shè)置圖標(biāo)的左上右下
// 默認(rèn)設(shè)置隱藏圖標(biāo)
setClearIconVisible(false);
// 設(shè)置焦點(diǎn)改變的監(jiān)聽(tīng)
setOnFocusChangeListener(this);
// 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽(tīng)
addTextChangedListener(this);
}
由于不能直接給EditText設(shè)置監(jiān)聽(tīng)事件,所以采用記錄點(diǎn)擊位置來(lái)模擬點(diǎn)擊事件,只記錄了魚(yú)圖標(biāo)的左右點(diǎn)擊。
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
判斷輸入框中是否有文字,動(dòng)態(tài)設(shè)置刪除圖標(biāo)的顯示和隱藏。
public void onFocusChange(View v, boolean hasFocus) {
this.hasFoucs = hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
如果輸入框中有文字 那么久繪制刪除圖標(biāo)
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
當(dāng)輸入框內(nèi)容發(fā)生變化的時(shí)候動(dòng)態(tài)改變刪除圖標(biāo)
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}
}
至此就完成了:當(dāng)屬框中沒(méi)有文本的時(shí)候 刪除圖標(biāo)隱藏 當(dāng)有文本輸入的時(shí)候,刪除圖標(biāo)顯示,點(diǎn)擊刪除圖標(biāo),清空文本內(nèi)容。
自定義InputType返回為”*”
設(shè)置密碼樣式要繼承PasswordTransformationMethod這個(gè)類(lèi)然后實(shí)現(xiàn)CharSequence方法去修改CharAt的返回值為“*”即可。
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
這里用于修改InputType的返回樣式
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
然后在主程序中初始化控件,在布局中設(shè)置android:password=”true”這一行代碼,以便在代碼中動(dòng)態(tài)設(shè)置密碼輸入的返回樣式。
et_pass_word = (ClearEditText) findViewById(R.id.et_pass_word); et_pass_word.setTransformationMethod(new EditTextBgToStar());
總結(jié):
在自定義的EditText中加入刪除圖標(biāo)的監(jiān)聽(tīng),由于不能直接設(shè)置,所以采用記錄按下的位置來(lái)模擬點(diǎn)擊事件??傮w實(shí)現(xiàn)思路就是在EditText的右邊畫(huà)一個(gè)刪除圖標(biāo),然后動(dòng)態(tài)設(shè)置顯示或隱藏,通過(guò)設(shè)置監(jiān)聽(tīng)事件,來(lái)動(dòng)態(tài)顯示,清除文本框中的文本。在自定義密碼返回樣式的時(shí)候,主要就是繼承PasswordTransformationMethod這個(gè)類(lèi),實(shí)現(xiàn)CharSequence方法,替換輸入樣式為自定義。
點(diǎn)擊下載源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
- Android控件系列之EditText使用方法
- android同時(shí)控制EditText輸入字符個(gè)數(shù)和禁止特殊字符輸入的方法
- Android中EditText實(shí)現(xiàn)不可編輯解決辦法
- Android定制自己的EditText輕松改變底線顏色
- Android中EditText顯示明文與密碼的兩種方式
- Android EditText實(shí)現(xiàn)扁平化的登錄界面
- Android高級(jí)xml布局之輸入框EditText設(shè)計(jì)
- Android自定義EditText實(shí)現(xiàn)登錄界面
- Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能
相關(guān)文章
從零開(kāi)始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
這篇文章主要介紹了android實(shí)現(xiàn)的計(jì)算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02
Android Activity 與Service進(jìn)行數(shù)據(jù)交互詳解
這篇文章主要介紹了Android Activity 與Service進(jìn)行數(shù)據(jù)交互的相關(guān)資料,在開(kāi)發(fā)Android App的時(shí)候經(jīng)常會(huì)使用這樣的功能,需要的朋友可以參考下2016-10-10
Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來(lái)實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09
android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android仿微信文章懸浮窗效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿微信文章懸浮窗效果的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-10-10
Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
這篇文章主要介紹了Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android取消EditText自動(dòng)獲取默認(rèn)焦點(diǎn)
本文主要介紹了Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android使用第三方服務(wù)器Bmob實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼
這篇文章主要介紹了Android使用第三方服務(wù)器Bmob實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的思路詳解,需要的朋友可以參考下2016-09-09

