自定义edittext,用于带删除的输入框和搜索框

直接上代码,呵呵,以下的
setCompoundDrawables是最关键的方法。这里要先获取布局文件里面的drawableRight的图片传给dRight。

package com.example.android_widget_byself_widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class EdittextCanClear extends EditText {
	private Drawable dRight;
	private Drawable dLeft;
	private Rect rect;

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

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

	public EdittextCanClear(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	// 初始化edittext 控件
	private void init() {
		// TODO Auto-generated method stub
		setEditTextImage();
		this.setOnFocusChangeListener(new Changlistener());

		// 内容监听
		addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				EdittextCanClear.this.setEditTextImage();
			}

			@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 boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		if (this.dRight != null && event.getAction() == 1 && this.isFocused()) {
			rect = this.dRight.getBounds();
			int i = (int) event.getX();
			if (i > this.getRight() - 2 * rect.width() - getPaddingRight()
					&& i < getRight() - getPaddingRight()) {
				setText("");
				event.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(event);
	}

	// 控制图片的显示
	private void setEditTextImage() {
		if (getText().toString().length() != 0 && this.isFocused()) {
			setCompoundDrawables(dLeft, null, this.dRight, null);
		} else {
			setCompoundDrawables(dLeft, null, null, null);
		}
	}

	// /**
	// * 设置显示的图片资源
	// */
	// private void setCompoundDrawable(Drawable left, Drawable top,
	// Drawable right, Drawable bottom) {
	// if (right != null && this.dRight == null) {
	// this.dRight = right;
	// }
	// this.dLeft = left;
	// super.setCompoundDrawables(left, top, right, bottom);
	// }

	@Override
	public void setCompoundDrawables(Drawable left, Drawable top,
			Drawable right, Drawable bottom) {
		// TODO Auto-generated method stub
		if (right != null && this.dRight == null) {
			this.dRight = right;
		}
		this.dLeft = left;
		super.setCompoundDrawables(left, top, right, bottom);
	}

	// 清楚缓存
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		this.dRight = null;
		this.rect = null;
		this.dLeft = null;

	}

	class Changlistener implements OnFocusChangeListener {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			EdittextCanClear.this.setEditTextImage();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值