admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
package com.wpc.library.widget;
 
import android.content.Context;
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 android.widget.EditText;
 
import com.wpc.lcjianlibrary.R;
 
/**
 * Created by weikou2015 on 2018/5/30.
 */
 
public class PhoneNumberClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
 
    private Drawable mClearDrawable;
    private boolean hasFocus;
 
    public PhoneNumberClearEditText(Context context) {
        this(context, null);
    }
 
    public PhoneNumberClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public PhoneNumberClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
 
    private void init() {
        // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)
        mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight
        if (mClearDrawable == null) {
            // 如果为空,即没有设置drawableRight,则使用R.mipmap.close这张图片
            mClearDrawable = getResources().getDrawable(R.drawable.ic_cancel);
        }
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
        // 默认隐藏图标
        setDrawableVisible(false);
    }
 
    /**
     * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
                int end = getWidth(); // 结束位置
                boolean available = (event.getX() > start) && (event.getX() < end);
                if (available) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }
 
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        if (hasFocus && getText().length() > 0) {
            setDrawableVisible(true); // 有焦点且有文字时显示图标
        } else {
            setDrawableVisible(false);
        }
    }
 
    @Override
    public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
        if (hasFocus) {
            setDrawableVisible(charSequence.length() > 0);
        }
        if (charSequence == null || charSequence.length() == 0) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0;i<charSequence.length();i++) {
            if (i != 3 && i != 8 && charSequence.charAt(i) == ' ') {
                continue;
            } else {
                stringBuilder.append(charSequence.charAt(i));
                if ((stringBuilder.length() == 4 || stringBuilder.length() == 9)
                        && stringBuilder.charAt(stringBuilder.length() - 1) != ' ') {
                    stringBuilder.insert(stringBuilder.length() - 1, ' ');
                }
            }
        }
        if (!stringBuilder.toString().equals(charSequence.toString())) {
            int index = start + 1;
            if (stringBuilder.charAt(start) == ' ') {
                if (count == 0) {
                    index++;
                } else {
                    index--;
                }
            } else {
                if (count == 1) {
                    index--;
                }
            }
            setText(stringBuilder.toString());
            setSelection(index);
        }
    }
 
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
 
    @Override
    public void afterTextChanged(Editable s) {
 
    }
 
    protected void setDrawableVisible(boolean visible) {
        Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }
 
}