package com.android.app_base.widget;
|
|
import android.content.Context;
|
import android.content.res.TypedArray;
|
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 androidx.annotation.DrawableRes;
|
import androidx.annotation.Nullable;
|
import androidx.appcompat.widget.AppCompatEditText;
|
import androidx.core.content.ContextCompat;
|
import androidx.core.graphics.drawable.DrawableCompat;
|
|
import com.android.app_base.R;
|
|
/**
|
* @author Ljj
|
* @date 2023.05.17. 17:00
|
* @desc
|
*/
|
public final class ClearEditText extends AppCompatEditText
|
implements View.OnTouchListener,
|
View.OnFocusChangeListener, TextWatcher {
|
|
private Drawable mClearDrawable;
|
|
@Nullable
|
private View.OnTouchListener mTouchListener;
|
@Nullable
|
private View.OnFocusChangeListener mFocusChangeListener;
|
|
public ClearEditText(Context context) {
|
this(context, null);
|
}
|
|
public ClearEditText(Context context, AttributeSet attrs) {
|
this(context, attrs, android.R.attr.editTextStyle);
|
}
|
|
@SuppressWarnings("all")
|
public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
|
super(context, attrs, defStyleAttr);
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ClearEditText);
|
mClearDrawable = a.getDrawable(R.styleable.ClearEditText_background);
|
if (mClearDrawable == null){
|
mClearDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.input_delete_ic));
|
}
|
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
|
setDrawableVisible(false);
|
super.setOnTouchListener(this);
|
super.setOnFocusChangeListener(this);
|
super.addTextChangedListener(this);
|
}
|
|
private void setDrawableVisible(boolean visible) {
|
if (mClearDrawable.isVisible() == visible) {
|
return;
|
}
|
|
mClearDrawable.setVisible(visible, false);
|
Drawable[] drawables = getCompoundDrawablesRelative();
|
setCompoundDrawablesRelative(
|
drawables[0],
|
drawables[1],
|
visible ? mClearDrawable : null,
|
drawables[3]);
|
}
|
|
@Override
|
public void setOnFocusChangeListener(@Nullable View.OnFocusChangeListener onFocusChangeListener) {
|
mFocusChangeListener = onFocusChangeListener;
|
}
|
|
@Override
|
public void setOnTouchListener(@Nullable View.OnTouchListener onTouchListener) {
|
mTouchListener = onTouchListener;
|
}
|
|
/**
|
* {@link View.OnFocusChangeListener}
|
*/
|
|
@Override
|
public void onFocusChange(View view, boolean hasFocus) {
|
if (hasFocus && getText() != null) {
|
setDrawableVisible(getText().length() > 0);
|
} else {
|
setDrawableVisible(true);
|
}
|
setDrawableVisible(getText() != null && getText().length() > 0);
|
if (mFocusChangeListener != null) {
|
mFocusChangeListener.onFocusChange(view, hasFocus);
|
}
|
}
|
|
/**
|
* {@link View.OnTouchListener}
|
*/
|
|
@Override
|
public boolean onTouch(View view, MotionEvent event) {
|
int x = (int) event.getX();
|
|
// 是否触摸了 Drawable
|
boolean touchDrawable = false;
|
// 获取布局方向
|
int layoutDirection = getLayoutDirection();
|
if (layoutDirection == LAYOUT_DIRECTION_LTR) {
|
// 从左往右
|
touchDrawable = x > getWidth() - mClearDrawable.getIntrinsicWidth() - getPaddingEnd() &&
|
x < getWidth() - getPaddingEnd();
|
} else if (layoutDirection == LAYOUT_DIRECTION_RTL) {
|
// 从右往左
|
touchDrawable = x > getPaddingStart() &&
|
x < getPaddingStart() + mClearDrawable.getIntrinsicWidth();
|
}
|
|
if (mClearDrawable.isVisible() && touchDrawable) {
|
if (event.getAction() == MotionEvent.ACTION_UP) {
|
setText("");
|
setDrawableVisible(false);
|
}
|
return true;
|
}
|
return mTouchListener != null && mTouchListener.onTouch(view, event);
|
}
|
|
/**
|
* {@link TextWatcher}
|
*/
|
|
@Override
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
if (isFocused()) {
|
setDrawableVisible(s.length() > 0);
|
}
|
}
|
|
@Override
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
@Override
|
public void afterTextChanged(Editable s) {}
|
|
}
|