自定义EditText

核心方法:1、setCompoundDrawables(Drawable left,Drawable top,Drawable right, Drawable bottom)//该方法控制的是EditText所带的清除内容的按钮的位置              
2、invalidate();//实质上是调用Canvas中的onDraw方法将控件绘制出来
实现思路:1、继承Edittext, 实现TextWatcher                 
2、初始化initEditText()

</pre><pre code_snippet_id="1900447" snippet_file_name="blog_20160926_1_7917568" name="code" class="html">private void initEditText() {
        dRight = ContextCompat.getDrawable(context,R.drawable.search_clear_normal);
        dRight.setBounds(0,0,dRight.getIntrinsicWidth(),dRight.getIntrinsicHeight());
        setEditTextDrawable();
        addTextChangedListener(this);

    }

    public void setEditTextDrawable() {
        Log.v("editText的内容--->>",getText().toString());
        if (getText().toString().length()==0) {
            setCompoundDrawables(null, null, null, null);
        } else {
            setCompoundDrawables(null, null, this.dRight, null);
        }
        invalidate();
    }
在实现过程中,我还重写了setCompoundDrawables()方法,

this.dRight = p3Drawable;
其中只用了这么一个语句,乖乖,搞了我好久,就是丫的不显示这个叉叉。什么鬼,浪费了我半天时间,原来只是自己和自己在赋值,根本就没有将控件渲染出来。就写一个invalidate()就ok了!!! 
源码:http://download.youkuaiyun.com/detail/poulfei/9639909


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值