android自定义文本框,后面带清空按钮

时间:2021-09-23 15:44:29

android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示

package com.qc.health.weight;

import com.qc.health.R;

import android.annotation.SuppressLint;
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.widget.EditText;

public class MyEditText extends EditText {
private final String TAG = "MyEditText";
private Drawable dRight;
private Rect rBounds;

// 构造器
public MyEditText(Context paramContext) {
super(paramContext);
initEditText();
}

public MyEditText(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
initEditText();
}

public MyEditText(Context paramContext, AttributeSet paramAttributeSet,
int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
initEditText();
}

// 初始化edittext 控件
@SuppressLint("ResourceAsColor")
private void initEditText() {
setEditTextDrawable();
this.setBackgroundResource(R.drawable.edittext_bg);
this.setTextColor(R.color.login_txt);
this.setHintTextColor(R.color.login_remind_txt);
this.setPadding(20, 3, 6, 3);
this.setTextSize(14);
this.setSingleLine(true);
addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
public void afterTextChanged(Editable paramEditable) {
}

public void beforeTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
}

public void onTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
MyEditText.this.setEditTextDrawable();
}
});
}

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

//失去焦点事件
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
//获得焦点,判断是否有内容
if (this.hasFocus() == true) {
//如果没有内容,则保护显示清空按钮,否则就显示
if (getText().toString().length() == 0){
setCompoundDrawables(null, null, null, null);
} else {
setCompoundDrawables(null, null, this.dRight, null);
}
}else{
//失去焦点,隐藏按钮
setCompoundDrawables(null, null, null, null);
}
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

protected void finalize() throws Throwable {
super.finalize();
this.dRight = null;
this.rBounds = null;
}

// 添加触摸事件
@SuppressWarnings("unused")
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
if ((this.dRight != null) && (paramMotionEvent.getAction() == 1) && getText().toString().length() > 0) {
this.rBounds = this.dRight.getBounds();
int i = (int) paramMotionEvent.getX();
int ix = (int) paramMotionEvent.getRawX();
// int e = (int)this.getX();
// int ex = (int)this.getRotationX();
int width = this.getWidth();
int left = (int)this.getLeft();
int right = (int)this.getRight();
// int marginWidth = right - this.rBounds.width()-10;
int marginWidth = width - this.rBounds.width()-5;
// int marginWidth = i - (getRight() - 3 * (this.rBounds.width()+10));
// if (marginWidth > -40 && marginWidth < 40) {
if (marginWidth < i && i <= width) {
setText("");
paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
}
}
return super.onTouchEvent(paramMotionEvent);
}

// 设置显示的图片资源
public void setCompoundDrawables(Drawable paramDrawable1,
Drawable paramDrawable2, Drawable paramDrawable3,
Drawable paramDrawable4) {
if (paramDrawable3 != null)
this.dRight = paramDrawable3;
super.setCompoundDrawables(paramDrawable1, paramDrawable2,
paramDrawable3, paramDrawable4);
}
}

注意使用的时候是这样的

<com.qc.health.weight.MyEditText
android:id="@id/login_etUserNameId"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:drawableRight="@drawable/del"
android:hint="@string/login_et_account_txt" />

android:drawableRight="@drawable/del"这个属性是必带的,是清空按钮的图片