Android開發(fā)實現(xiàn)帶清空按鈕的EditText示例
本文實例講述了Android開發(fā)實現(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ā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android 自定義EditText輸入框帶清空按鈕
- Android實現(xiàn)帶有刪除按鈕的EditText示例代碼
- Android 帶有刪除按鈕的EditText
- Android如何自定義EditText下劃線?
- Android EditText自定義樣式的方法
- Android UI設(shè)計系列之自定義EditText實現(xiàn)帶清除功能的輸入框(3)
- Android自定義EditText右側(cè)帶圖片控件
- Android中自定義的dialog中的EditText無法彈出輸入法解決方案
- Android如何自定義EditText光標(biāo)與下劃線顏色詳解
- Android實現(xiàn)自定義帶刪除功能的EditText實例
相關(guān)文章
Android開源AndroidSideMenu實現(xiàn)抽屜和側(cè)滑菜單
這篇文章主要為大家詳細(xì)介紹了Android開源AndroidSideMenu實現(xiàn)抽屜和側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02android編程實現(xiàn)添加文本內(nèi)容到sqlite表中的方法
這篇文章主要介紹了android編程實現(xiàn)添加文本內(nèi)容到sqlite表中的方法,結(jié)合實例較為詳細(xì)的分析了Android針對txt文本文件的讀取及SQL數(shù)據(jù)庫操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11Android開發(fā)之Android.mk模板的實例詳解
這篇文章主要介紹了Android開發(fā)之Android.mk模板的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Android SeekBar控制視頻播放進度實現(xiàn)過程講解
這篇文章主要介紹了Android SeekBar控制視頻播放進度實現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04Android開發(fā)在RecyclerView上面實現(xiàn)"拖放"和"滑動刪除"-2
這篇文章主要介紹了Android開發(fā)在RecyclerView上面實現(xiàn)"拖放"和"滑動刪除"(二)功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03