Android自定义右边带删除按钮的EditText

在开发Android应用程序时,有时需要EditText的的右侧提供一个删除按钮,当输入框有内容是显示右边的删除按钮,当输入框没有内容时隐藏右边的删除按钮。要实现这样的功能我们可以继承已有的EditText来实现,代码很简单,下面贴上代码:

public class LoginEditText extends EditText {
	private Drawable mClearDrawble;
	private Rect mBounds;

	public LoginEditText(Context context) {
		super(context);
		initEditText();
	}

	public LoginEditText(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		initEditText();
	}

	public LoginEditText(Context context, AttributeSet attributeSet,int paramInt) {
		super(context, attributeSet, paramInt);
		initEditText();
	}

	private void initEditText() {
		setEditTextDrawable();
		addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable editable) {
			}

			@Override
			public void beforeTextChanged(CharSequence charSequence,
					int arg1, int arg2, int arg3) {
			}

			@Override
			public void onTextChanged(CharSequence paramCharSequence,
					int arg1, int arg2, int arg3) {
					LoginEditText.this.setEditTextDrawable();
			}
		});
	}

	public void setEditTextDrawable() {
		if (getText().toString().length() == 0) {
			setCompoundDrawables(null, null, null, null);
		} else {
			setCompoundDrawables(null, null, mClearDrawble, null);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mClearDrawble = null;
		mBounds = null;
	}

	@Override
	public boolean onTouchEvent(MotionEvent motionEvent) {
		if ((mClearDrawble != null) && (motionEvent.getAction() == MotionEvent.ACTION_UP)) {
			mBounds = mClearDrawble.getBounds();
			int i = (int) motionEvent.getRawX();
			
			if (i > getRight() - 3 *  mBounds.width()) {
				setText("");
				motionEvent.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(motionEvent);
	}
	
	@Override
	public void setCompoundDrawables(Drawable left,Drawable top, Drawable right,Drawable bottom) {
		if (right != null)
			mClearDrawble = right;
		super.setCompoundDrawables(left,top,right,bottom);
	}
}



在布局文件中的使用如下:‘

<span style="color:#ff0000;"> <com.example.userlogin.LoginEditText</span>
            android:id="@+id/account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
           <span style="color:#ff0000;"> android:drawableRight="@drawable/ic_clear_normal"</span>
            android:background="@drawable/edit_text_selector"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:textColor="#ffffff" />

      <span style="color:#ff0000;">  <com.example.userlogin.LoginEditText</span>
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edit_text_selector"
           <span style="color:#ff0000;"> android:drawableRight="@drawable/ic_clear_normal"</span>
            android:inputType="textPassword"
            android:textColor="#ffffff" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值