Android實現(xiàn)自定義帶刪除功能的EditText實例
1.說明
自定義帶刪除功能的EditText有兩種方法,第一種是用組合視圖的方法,即在一個view視圖里面左側(cè)放置一個EditText,右側(cè)放置一個ImageView,但是這樣增加了視圖的層次,而且對輸入內(nèi)容的長度要做一定的處理。
第二種是重新定義EditText組件,增加相應(yīng)的事件處理,即可達(dá)到很好的效果,效果圖如下:
2.ClearEditText的JAVA類文件
/** * @說明: 自定義帶刪除按鈕的EditText * */ public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { //EditText右側(cè)的刪除按鈕 private Drawable mClearDrawable; private boolean hasFoucs; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認(rèn)的圖片,獲取圖片的順序是左上右下(0,1,2,3,) mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources().getDrawable( R.drawable.edit_delete); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); // 默認(rèn)設(shè)置隱藏圖標(biāo) setClearIconVisible(false); // 設(shè)置焦點改變的監(jiān)聽 setOnFocusChangeListener(this); // 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽 addTextChangedListener(this); } /* @說明:isInnerWidth, isInnerHeight為ture,觸摸點在刪除圖標(biāo)之內(nèi),則視為點擊了刪除圖標(biāo) * event.getX() 獲取相對應(yīng)自身左上角的X坐標(biāo) * event.getY() 獲取相對應(yīng)自身左上角的Y坐標(biāo) * getWidth() 獲取控件的寬度 * getHeight() 獲取控件的高度 * getTotalPaddingRight() 獲取刪除圖標(biāo)左邊緣到控件右邊緣的距離 * getPaddingRight() 獲取刪除圖標(biāo)右邊緣到控件右邊緣的距離 * isInnerWidth: * getWidth() - getTotalPaddingRight() 計算刪除圖標(biāo)左邊緣到控件左邊緣的距離 * getWidth() - getPaddingRight() 計算刪除圖標(biāo)右邊緣到控件左邊緣的距離 * isInnerHeight: * distance 刪除圖標(biāo)頂部邊緣到控件頂部邊緣的距離 * distance + height 刪除圖標(biāo)底部邊緣到控件頂部邊緣的距離 */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] != null) { int x = (int)event.getX(); int y = (int)event.getY(); Rect rect = getCompoundDrawables()[2].getBounds(); int height = rect.height(); int distance = (getHeight() - height)/2; boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight()); boolean isInnerHeight = y > distance && y <(distance + height); if (isInnerWidth && isInnerHeight) { this.setText(""); } } } return super.onTouchEvent(event); } /** * 當(dāng)ClearEditText焦點發(fā)生變化的時候, * 輸入長度為零,隱藏刪除圖標(biāo),否則,顯示刪除圖標(biāo) */ @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFoucs = hasFocus; if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } protected void setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if (hasFoucs) { setClearIconVisible(s.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }
3.引用ClearEditText的XML文件
<com.once.android_ui.selfview.ClearEditText android:id="@+id/user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/user_name" android:drawablePadding="7dp" android:hint="@string/name_tip" android:singleLine="true" android:textSize="17sp" > <requestFocus /> </com.once.android_ui.selfview.ClearEditText>
附件是圖片資源文件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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開發(fā)實現(xiàn)帶清空按鈕的EditText示例
相關(guān)文章
Kotlin中ListView與RecyclerView的應(yīng)用講解
這篇文章主要介紹了Kotlin中ListView與RecyclerView的應(yīng)用講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Android編程實現(xiàn)分頁加載ListView功能示例
這篇文章主要介紹了Android編程實現(xiàn)分頁加載ListView功能,結(jié)合實例形式分析了listview分頁加載的原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2017-02-02Android?debug包運行正常release包崩潰的解決辦法
這篇文章主要介紹了Android?debug包運行正常,release包崩潰解決辦法,文中通過代碼示例介紹的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下2024-04-04Flutter調(diào)用Android和iOS原生代碼的方法示例
這篇文章主要給大家介紹了關(guān)于Flutter調(diào)用Android和iOS原生代碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04