实现输入框右侧圆形X图标删除输入框内容,以及获取焦点消除hint

这个博客介绍了一个自定义EditText的实现,通过设置右侧的圆形X图标实现删除输入内容的功能,并在获取焦点时自动消除hint。博主分享了经过调试适配自己项目的代码,强调应用需要声明attrs属性dismissHintOnFocus和右侧drawableRight。博客鼓励读者根据需求进行调整,并欢迎反馈和讨论。
package cn.mobile.renrentou.main.view.edittext;

import cn.mobile.renrentou.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;

public class CustomEditText extends EditText {
	private Drawable dRight;
	private boolean dismissHintOnFocus = false;

	public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.initAttrs(context, attrs);
	}

	public CustomEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.initAttrs(context, attrs);
	}

	public CustomEditText(Context context) {
		super(context);
	}
	
	@SuppressLint("Recycle")
	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
		dismissHintOnFocus = typedArray.getBoolean(R.styleable.CustomEditText_dismissHintOnFocus, true);
	}

	@Override
	public void setCompoundDrawables(Drawable left, Drawable top,
			Drawable right, Drawable bottom) {
		if (right != null) {
			dRight = right;
		}
		super.setCompoundDrawables(left, top, right, bottom);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP && dRight != null) {
			final int x = (int) event.getX();
			final int y = (int) event.getY();
			// check to make sure the touch event was within the bounds of the
			int w = getWidth();
			if (x <= w - this.getPaddingRight()
					&& x >= (w - this.getPaddingRight() - dRight
							.getIntrinsicWidth())) {
				this.setText("");
				// use this to prevent the keyboard from coming up

				event.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		Log.d("CustomEdit", "onFocusChaged: " + dismissHintOnFocus);
		if (!dismissHintOnFocus) {
			super.onFocusChanged(focused, direction, previouslyFocusedRect);
			return;
		}
		String hint;
		if (focused) {
			CharSequence cs = this.getHint();
			cs = (cs == null) ? "" : cs;
			hint = cs.toString();
			if (null != hint && !"".equals(hint)) {
				this.setTag(hint);
				this.setHint("");
			} else {
				this.setTag("");
			}
		} else {
			hint = this.getTag().toString();
			this.setHint(hint);
		}

		super.onFocusChanged(focused, direction, previouslyFocusedRect);
	}

	@Override
	protected void finalize() throws Throwable {
		dRight = null;
		super.finalize();
	}
}
内容很单一,参考别人的成品,但是报大量错误,经过修改后调试出在我的项目中不报错的版本,应用需要声明attrs属性,dismissHintOnFocus, 右侧x图片则声明drawableRight  可能 有许多冗余代码和bug,大家根据自己的应用自己调试即可。如果有错误或者有其他想法思路,请大家留言不吝赐教。尽量不要用wrap_content标记宽度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值