EditText自定义重写

来源:互联网 发布:linux网络工程师 编辑:程序博客网 时间:2024/06/05 17:26
</pre><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"><span style="white-space:pre"></span>今天试着完成自定义EditText的重写,在后面加了一个图标,有文字输入的话可以直接点击图标删掉所有文字。测试了一下 ,应该没有什么问题了。</span><span class="token lf" style="box-sizing: border-box; color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap; outline: none !important;"></span><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"></span></p><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"><span style="white-space:pre"></span>说一下思路先:新建一个类继承EditText重写三个构造方法,实现TextWatcher接口,监听EditText上面的字符串情况,需要重写onTextChanged方法,这个方法里可以得到当前实时的editText的字符串,取出字符串的大小给setImage方法判断,如果有字符串的话给editText后面显示出一个图标,没有的话不显示 显示图标用到的是setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawable, null);这里的四个参数分别表示在editText的左,上,右,下显示的图片,null则不显示。</span><span class="token lf" style="box-sizing: border-box; color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap; outline: none !important;"></span><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"></span></p><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"><span style="white-space:pre"></span>然后我们要重写onTouchEvent方法来处理点击图标之后清空输入框字符串的事件。因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件,当点击事件的x坐标在图标的范围之内把输入框置空。这里涉及到一个getTotalPaddingRight() 官方注解是Returns the total right padding of the view, including the right Drawable if any,返回视图右边距合计,包括右侧的可绘制对象(如果存在),这里可以粗略的看做是图标的宽度.还有getPaddingRight(),Returns the right padding of this view.可以确定为图标的左右边界的x坐标范围,在这个范围内使其置空。</span></p><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"></span></p><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"></span></p><p><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace; font-size: 16.2000007629395px; white-space: pre-wrap;"></span><pre name="code" class="java">package com.example.edittext;import com.example.testmyview.R;import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.EditText;public class MyEditText2 extends EditText implements TextWatcher{private int mLength;private Drawable mDrawable;public MyEditText2(Context context) {super(context);// TODO Auto-generated constructor stubinit();}public MyEditText2(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubinit(); }public MyEditText2(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinit();}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub}@Overridepublic void afterTextChanged(Editable s) {// TODO Auto-generated method stub}@Overridepublic void onTextChanged(CharSequence text, int start,int lengthBefore, int lengthAfter) {// TODO Auto-generated method stubsuper.onTextChanged(text, start, lengthBefore, lengthAfter);mLength = text.length();setImage();}public void init(){if(mDrawable==null){if(!isInEditMode()){//造成错误的代码段mDrawable = this.getResources().getDrawable(R.drawable.cancel_press);}}setImage();}public void setImage(){if(mLength>0){setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawable, null);}else{setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);}}/** * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 当我们按下的位置 在 EditText的宽度 - * 图标到控件右边的间距 - 图标的宽度 和 EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑 */@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubif(event.getAction()==MotionEvent.ACTION_UP){boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));//boolean touchable = event.getX() > (getPaddingLeft()) && (event.getX() < (getTotalPaddingLeft()));if(touchable){setText("");}}return super.onTouchEvent(event);}}





1 0
原创粉丝点击