国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)自定義帶刪除功能的EditText實例

2019-12-12 02:37:41
字體:
供稿:網(wǎng)友

1.說明

自定義帶刪除功能的EditText有兩種方法,第一種是用組合視圖的方法,即在一個view視圖里面左側(cè)放置一個EditText,右側(cè)放置一個ImageView,但是這樣增加了視圖的層次,而且對輸入內(nèi)容的長度要做一定的處理。

第二種是重新定義EditText組件,增加相應(yīng)的事件處理,即可達到很好的效果,效果圖如下:

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è)置我們就使用默認的圖片,獲取圖片的順序是左上右下(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());     // 默認設(shè)置隱藏圖標     setClearIconVisible(false);     // 設(shè)置焦點改變的監(jiān)聽     setOnFocusChangeListener(this);     // 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽     addTextChangedListener(this);   }        /* @說明:isInnerWidth, isInnerHeight為ture,觸摸點在刪除圖標之內(nèi),則視為點擊了刪除圖標    * event.getX() 獲取相對應(yīng)自身左上角的X坐標    * event.getY() 獲取相對應(yīng)自身左上角的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> 

附件是圖片資源文件。



以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 吴忠市| 波密县| 阳春市| 中卫市| 青州市| 焉耆| 通州市| 若羌县| 广平县| 岳阳市| 射洪县| 武鸣县| 孝感市| 平潭县| 吉林省| 黔西县| 长垣县| 陆良县| 慈溪市| 台南市| 神农架林区| 罗源县| 会同县| 沛县| 眉山市| 全州县| 密云县| 德安县| 通州市| 泰兴市| 时尚| 巴青县| 弋阳县| 固安县| 丹棱县| 阜康市| 武威市| 澜沧| 阳原县| 堆龙德庆县| 大冶市|