美文网首页
自定义可清空内容的EditText

自定义可清空内容的EditText

作者: Smart_Arvin | 来源:发表于2016-06-27 13:28 被阅读550次

    在开发过程中不可避免的总会遇到比如登录注册、用户信息修改等,这时候又是不可避免的会用到EditText控件。这个控件的使用频率虽然几乎类似我们吃饭用“筷子”的频率,but能不能用出花样,用的有技术就看你有没有下功夫喽。

    代码实现:

    import android.content.Context;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.EditText;
     
    import com.ktc.sczwdemo.R;
    import com.ktc.utils.ResUtils;
     
    /**
     * @Date 2016-5-13 下午2:23:23
     * @Author Arvin
     * @Description 可删除且带有drawableLeft的EditText
     */ 
    public class EditWithDelView extends EditText {
        private final static String TAG = "EditWithDelView";
        private Drawable imgDisable;
        private Drawable imgAble;
        private Context mContext;
     
        public EditWithDelView(Context context) {
            super(context);
            mContext = context;
            init();
        }
     
        public EditWithDelView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mContext = context;
            init();
        }
     
        public EditWithDelView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            init();
        }
         
        private void init() {
            imgAble = ResUtils.getDrawable(R.drawable.delete);
            addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {}
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                @Override
                public void afterTextChanged(Editable s) {
                    setDrawable();
                }
            });
             
            this.setOnFocusChangeListener(new OnFocusChangeListener() {
                 
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(hasFocus&&length()>=1){
                        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, imgAble, null);
                    }else{
                        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
                    }
                }
            });
        }
         
        //根据字符长度加载提示Drawable
        private void setDrawable() {
            if(length() < 1){
                setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
            }else if(isFocused()){
                setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, imgAble, null);
            }
        }
         
         //响应触摸点击事件
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
                int eventX = (int) event.getRawX();
                int eventY = (int) event.getRawY();
                Rect rect = new Rect();
                getGlobalVisibleRect(rect);
                rect.left = rect.right - 50;
                rect.top = rect.bottom - 50;
                if(rect.contains(eventX, eventY)){ 
                    setText("");
                    setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
                }
            }
            return super.onTouchEvent(event);
        }
     
        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }
    

    里面可根据需要替换imgDisable和imgAble两个资源。

    • 使用方法:在Xml中像普通EditText一样直接引用即可

    谨以作为开发记录,如果有帮到您,记得点赞哦

    相关文章

      网友评论

          本文标题:自定义可清空内容的EditText

          本文链接:https://www.haomeiwen.com/subject/ftmzdttx.html