核心方法: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