Linjiajia
2023-07-25 82e57df230ecb744af6c8865f80870ba03c86d89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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) {}
 
}