可清空文本的EditText

时间:2023-03-08 23:44:58
可清空文本的EditText

可清空文本的EditText

代码如下:

 public class DeleteEditText extends EditText {

     private Context mContext;

     //删除图标
private Drawable drawableDelete; public DeleteEditText(Context context) {
super(context);
mContext = context;
init();
} public DeleteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
} public DeleteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
} private void init() {
drawableDelete = mContext.getResources().getDrawable(R.drawable.ic_inputbox_clear);
drawableDelete.setBounds(0, 0, drawableDelete.getIntrinsicWidth(), drawableDelete.getIntrinsicHeight());
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override
public void afterTextChanged(Editable s) {
setIcon(length() > 0);
}
});
setIcon(false);
} @Override
public boolean onTouchEvent(MotionEvent event) {
if (drawableDelete != null && event.getAction() == MotionEvent.ACTION_UP) {
//获取触摸点坐标
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
//获取整个EdtiText的可见区域
Rect rect = new Rect();
getGlobalVisibleRect(rect);
//修改区域为图标区域
rect.left = rect.right - 80;
if (rect.contains(eventX, eventY)) {
//触摸点在图标区域,清空文本
setText("");
}
}
return super.onTouchEvent(event);
} //根据是否有内容来绘制图标
private void setIcon(boolean visible) {
Drawable right = visible ? drawableDelete : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
}