Android開發(fā)實(shí)現(xiàn)帶清空按鈕的EditText示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)帶清空按鈕的EditText。分享給大家供大家參考,具體如下:
一、效果圖:

二、具體代碼:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.gdc.control.R;
public class ClearableEditText extends AppCompatEditText implements View.OnTouchListener, View.OnFocusChangeListener, TextWatcher {
private Drawable clearTextIcon;
private OnFocusChangeListener mOnFocusChangeListener;
private OnTouchListener mOnTouchListener;
private boolean canClear = false;
public ClearableEditText(final Context context) {
super(context);
init(context);
}
public ClearableEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ClearableEditText(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@Override
public void setOnFocusChangeListener(final OnFocusChangeListener
onFocusChangeListener) {
mOnFocusChangeListener = onFocusChangeListener;
}
@Override
public void setOnTouchListener(final OnTouchListener onTouchListener) {
mOnTouchListener = onTouchListener;
}
private void init(final Context context) {
final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_clear_edittext);
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
clearTextIcon = wrappedDrawable;
clearTextIcon.setBounds(0, 0, clearTextIcon.getIntrinsicWidth(),
clearTextIcon.getIntrinsicHeight());
setClearIconVisible(false);
super.setOnTouchListener(this);
super.setOnFocusChangeListener(this);
addTextChangedListener(this);
}
@Override
public void onFocusChange(final View view, final boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
setCanClear(true);
}
if (mOnFocusChangeListener != null) {
mOnFocusChangeListener.onFocusChange(view, hasFocus);
}
}
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
final int x = (int) motionEvent.getX();
if (x > getWidth() - getPaddingRight() - clearTextIcon.getIntrinsicWidth()) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
if (clearTextIcon.isVisible()) {
setError(null);
setText("");
} else if (isCanClear()) {
setCanClear(false);
setError(null);
setText("");
}
}
return true;
} else {
return mOnTouchListener != null && mOnTouchListener.onTouch(view,
motionEvent);
}
}
@Override
public final void onTextChanged(final CharSequence s, final int start, final
int before, final int count) {
if (isFocused()) {
setClearIconVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
private void setClearIconVisible(final boolean visible) {
clearTextIcon.setVisible(visible, false);
final Drawable[] compoundDrawables = getCompoundDrawables();
setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], visible ?
clearTextIcon :
null, compoundDrawables[3]);
}
public synchronized boolean isCanClear() {
return canClear;
}
public synchronized void setCanClear(boolean canClear) {
this.canClear = canClear;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 自定義EditText輸入框帶清空按鈕
- Android實(shí)現(xiàn)帶有刪除按鈕的EditText示例代碼
- Android 帶有刪除按鈕的EditText
- Android如何自定義EditText下劃線?
- Android EditText自定義樣式的方法
- Android UI設(shè)計(jì)系列之自定義EditText實(shí)現(xiàn)帶清除功能的輸入框(3)
- Android自定義EditText右側(cè)帶圖片控件
- Android中自定義的dialog中的EditText無(wú)法彈出輸入法解決方案
- Android如何自定義EditText光標(biāo)與下劃線顏色詳解
- Android實(shí)現(xiàn)自定義帶刪除功能的EditText實(shí)例
相關(guān)文章
Android編程使用pull方式解析xml格式文件的方法詳解
這篇文章主要介紹了Android編程使用pull方式解析xml格式文件的方法,結(jié)合實(shí)例形式分析了Android調(diào)用pull解析器操作xml格式文件的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法
這篇文章主要給大家介紹了關(guān)于Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android View移動(dòng)的3種方式總結(jié)
這篇文章主要給大家介紹了Android View移動(dòng)的三種方式,在介紹這三種方式之前先介紹了Android坐標(biāo)系的定義規(guī)則以及View的一些位置參數(shù)。有需要的朋友們可以參考借鑒。2016-09-09
Android MarginDesign控件TabLayout導(dǎo)航欄使用詳解
這篇文章主要為大家詳細(xì)介紹了Android MarginDesign控件TabLayout導(dǎo)航欄使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的方法
項(xiàng)目需求如果在項(xiàng)目中第一行文字需要添加布局的情況我們應(yīng)該怎么做呢,經(jīng)過(guò)一番考慮和查找我最終選擇了縮進(jìn)的方式解決這個(gè)問(wèn)題,這篇文章主要給大家介紹了關(guān)于Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的相關(guān)資料,需要的朋友可以參考下2022-04-04

