1.說明
成都創(chuàng)新互聯(lián)公司是一家專注于網站制作、網站設計與策劃設計,溫泉網站建設哪家好?成都創(chuàng)新互聯(lián)公司做網站,專注于網站建設十載,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:溫泉等地區(qū)。溫泉做網站價格咨詢:18980820575
自定義帶刪除功能的EditText有兩種方法,第一種是用組合視圖的方法,即在一個view視圖里面左側放置一個EditText,右側放置一個ImageView,但是這樣增加了視圖的層次,而且對輸入內容的長度要做一定的處理。
第二種是重新定義EditText組件,增加相應的事件處理,即可達到很好的效果,效果圖如下:
2.ClearEditText的JAVA類文件
/** * @說明: 自定義帶刪除按鈕的EditText * */ public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { //EditText右側的刪除按鈕 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,假如沒有設置我們就使用默認的圖片,獲取圖片的順序是左上右下(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()); // 默認設置隱藏圖標 setClearIconVisible(false); // 設置焦點改變的監(jiān)聽 setOnFocusChangeListener(this); // 設置輸入框里面內容發(fā)生改變的監(jiān)聽 addTextChangedListener(this); } /* @說明:isInnerWidth, isInnerHeight為ture,觸摸點在刪除圖標之內,則視為點擊了刪除圖標 * event.getX() 獲取相對應自身左上角的X坐標 * event.getY() 獲取相對應自身左上角的Y坐標 * getWidth() 獲取控件的寬度 * getHeight() 獲取控件的高度 * getTotalPaddingRight() 獲取刪除圖標左邊緣到控件右邊緣的距離 * getPaddingRight() 獲取刪除圖標右邊緣到控件右邊緣的距離 * isInnerWidth: * getWidth() - getTotalPaddingRight() 計算刪除圖標左邊緣到控件左邊緣的距離 * getWidth() - getPaddingRight() 計算刪除圖標右邊緣到控件左邊緣的距離 * isInnerHeight: * distance 刪除圖標頂部邊緣到控件頂部邊緣的距離 * distance + height 刪除圖標底部邊緣到控件頂部邊緣的距離 */ @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); } /** * 當ClearEditText焦點發(fā)生變化的時候, * 輸入長度為零,隱藏刪除圖標,否則,顯示刪除圖標 */ @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>
附件是圖片資源文件。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文標題:Android實現(xiàn)自定義帶刪除功能的EditText實例
文章地址:http://aaarwkj.com/article4/gjojie.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站改版、、網站設計公司、網頁設計公司、商城網站、Google
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)