EditText自定义重写

</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 stub
		init();
	}

	public MyEditText2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(); 
	}

	public MyEditText2(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}
	
	

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence text, int start,
			int lengthBefore, int lengthAfter) {
		// TODO Auto-generated method stub
		super.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的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值