博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android控件库(1)-带删除功能的EditText
阅读量:6544 次
发布时间:2019-06-24

本文共 2106 字,大约阅读时间需要 7 分钟。

 

DJEditText.java

/** * Created by xp.chen on 2016/11/25. */public class DJEditText extends AppCompatEditText {    private static final int DRAWABLE_LEFT = 0;    private static final int DRAWABLE_TOP = 1;    private static final int DRAWABLE_RIGHT = 2;    private static final int DRAWABLE_BOTTOM = 3;    private Drawable mClearDrawable;    public DJEditText(Context context) {        super(context);        init();    }    public DJEditText(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public DJEditText(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mClearDrawable = getResources().getDrawable(R.drawable.app_clear_btn_bg_selector);    }    @Override    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {        super.onTextChanged(text, start, lengthBefore, lengthAfter);        setClearIconVisible(hasFocus() && length() > 0);    }    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {        super.onFocusChanged(focused, direction, previouslyFocusedRect);        setClearIconVisible(focused && length() > 0);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_UP:                Drawable drawable = getCompoundDrawables()[DRAWABLE_RIGHT];                if (drawable != null && event.getX() <= (getWidth() - getPaddingRight()) && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {                    setText("");                }                break;            default:                break;        }        return super.onTouchEvent(event);    }    public void setClearIconVisible(boolean visible) {        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[DRAWABLE_LEFT], getCompoundDrawables()[DRAWABLE_TOP]                ,visible ? mClearDrawable : null, getCompoundDrawables()[DRAWABLE_BOTTOM]);    }}

 

最终效果:

 

转载地址:http://hgodo.baihongyu.com/

你可能感兴趣的文章
管理员权限
查看>>
eclipse插件
查看>>
C# Note3:大话Ninject
查看>>
[转] 安装npm全局包提示权限不够
查看>>
Nodejs基础中间件Connect
查看>>
Silverlight Validation 数据验证DataAnnotation机制和调试技巧
查看>>
Webstrom卡顿问题解决
查看>>
又一次凡客面试体验_2
查看>>
Python3基础笔记--装饰器
查看>>
call和apply的作用和区别
查看>>
Google's Python Class
查看>>
sizeof的主要用法
查看>>
【杂谈】中科大参观有感
查看>>
javascript中window.open()与window.location.href的区别
查看>>
Java基础之equals和hashCode方法
查看>>
Deep Learning
查看>>
C++11 里lambda表达式的学习
查看>>
How Tomcat works — 七、tomcat发布webapp
查看>>
机器学习 — 文档过滤
查看>>
部分和问题
查看>>