首页 > 代码库 > 自定义EditText,带删除按键

自定义EditText,带删除按键

带清空按键,还可左右加图片,默认的是加了清空图片。

清空内容是判断的手势点击EditText的范围。

自定义EditText代码如下:

import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.EditText;/** * Created by Administrator on 2014/9/15. */    public class ClearEditText extends EditText implements TextWatcher,        View.OnFocusChangeListener {        /**         * 左右两侧图片资源         */        private Drawable left, right;
 
        /** 
         * 添加 ClearListener 可以实现搜索下面界面数据的清空
         */ 
        private ClearListener mClearListener;        /**         * 是否获取焦点,默认没有焦点         */        private boolean hasFocus = false;        /**         * 手指抬起时的X坐标         */        private int xUp = 0;        private boolean hasMyListener = false;        public interface ClearListener{            public void doChange();        }       public void setMyListener(ClearListener clearListener){        mClearListener = clearListener;        hasMyListener = true;       }    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);            initWedgits();        }        private void initWedgits() {            try {                // 获取drawableLeft图片,如果在布局文件中没有定义drawableLeft属性,则此值为空                left = getCompoundDrawables()[0];                // 获取drawableRight图片,如果在布局文件中没有定义drawableRight属性,则此值为空                right = getCompoundDrawables()[2];                initDatas();            } catch (Exception e) {                e.printStackTrace();            }        }        /**         * 初始化数据         */        private void initDatas() {            try {                // 第一次显示,隐藏删除图标                setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);                addListeners();            } catch (Exception e) {                e.printStackTrace();            }        }        /**         * 添加事件监听         */        private void addListeners() {            try {                setOnFocusChangeListener(this);                addTextChangedListener(this);            } catch (Exception e) {                e.printStackTrace();            }        }        @Override        public void beforeTextChanged(CharSequence s, int start, int count,                                      int after) {        }        @Override            public void onTextChanged(CharSequence s, int start, int before, int after) {                if (hasFocus) {                    if (TextUtils.isEmpty(s)) {                        // 如果为空,则不显示删除图标                        setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);                        if(hasMyListener){                            mClearListener.doChange();                        }                    } else {                        // 如果非空,则要显示删除图标                        if (null == right) {                            right = getCompoundDrawables()[2];                        }                        setCompoundDrawablesWithIntrinsicBounds(left, null, right, null);                    }                }        }    @Override        public boolean onTouchEvent(MotionEvent event) {            try {                switch (event.getAction()) {                    case MotionEvent.ACTION_UP:                        // 获取点击时手指抬起的X坐标                        xUp = (int) event.getX();                        // 当点击的坐标到当前输入框右侧的距离小于等于getCompoundPaddingRight()的距离时,则认为是点击了删除图标                        // getCompoundPaddingRight()的说明:Returns the right padding of the view, plus space for the right Drawable if any.                        if ((getWidth() - xUp) <= getCompoundPaddingRight()) {                            if (!TextUtils.isEmpty(getText().toString())) {                                setText("");                            }                        }                        break;                    default:                        break;                }            } catch (Exception e) {                e.printStackTrace();            }            return super.onTouchEvent(event);        }        @Override        public void afterTextChanged(Editable s) {        }        @Override        public void onFocusChange(View v, boolean hasFocus) {            try {                this.hasFocus = hasFocus;            } catch (Exception e) {                e.printStackTrace();            }        }    }

 

布局见中的实现:

<com.tuge.zonghengda_vector.widget.ClearEditText              android:layout_toLeftOf="@id/search_button"              android:layout_width="match_parent"              android:layout_height="40dp"              android:layout_centerVertical="true"              android:layout_marginLeft="8dp"              android:layout_marginRight="8dp"              android:paddingLeft="8dp"              android:paddingRight="8dp"              android:hint="请输入运单号或订舱号"              android:drawablePadding="8dp"              android:textSize="16sp"              android:textColorHint="#ffa9a9a9"              android:singleLine="true"              android:id="@+id/search_editText"              android:drawableLeft="@drawable/ico_search_small"              android:drawableRight="@drawable/text_clear_selector"              android:background="@drawable/search_background"/>

ico_search_small:ico_search_small

text_clear_selector:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/ico_text_clear"/>    <item android:drawable="@drawable/ico_text_clear_click"          android:state_pressed="true"/>    <item   android:state_focused="true" android:drawable="@drawable/ico_text_clear_click"/></selector>

ico_text_clear_click:ico_text_clear_click

 

最后在代码中获取即可。

自定义EditText,带删除按键