wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.mm.android.deviceaddmodule.mobilecommon.widget;
 
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.view.View;
 
import com.mm.android.deviceaddmodule.mobilecommon.utils.StringUtils;
 
public class ClearAutoCompleteText extends android.support.v7.widget.AppCompatAutoCompleteTextView {// delete the onFocusChangeListener,
                                                         // incorrect use, at 20150512
    private Drawable imgLeftFoucus = null;
 
    private Drawable imgLeftUnFoucus = null;
 
    private Drawable imgRightFoucus = null;
 
    private Drawable imgRightUnFoucus = null;
 
    private Context context;
 
    private int mLimitedLen = -1;
 
    private boolean mbUnregFilter = false;
 
    private boolean mbPWDFilter = false;
 
    private boolean mbFocus = false;
 
    private int errorTip;
 
    private TextWatcher mTextWatcher = 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) {
            ClearAutoCompleteText.this.removeTextChangedListener(mTextWatcher);
            filter(s);
            setDrawable();
            ClearAutoCompleteText.this.addTextChangedListener(mTextWatcher);
        }
    };
 
    public ClearAutoCompleteText(Context context) {
        super(context);
        this.context = context;
        init();
    }
 
    public ClearAutoCompleteText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }
 
    public ClearAutoCompleteText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }
 
    private void init() {
        addTextChangedListener(mTextWatcher);
        // setOnFocusChangeListener(this); //not need, deleted at 20150512
        setDrawable();
    }
 
    /**
     * 输入框是否可以复制|粘贴
     * 
     * @param copyAble
     */
    public void setCopyAble(boolean copyAble) {
        this.setLongClickable(copyAble);
    }
 
    /**
     * 设置编辑框前后图像
     * 
     * @param leftFocusResId
     *            (获取焦点时显示)当为0时表示不显示,其它则取资源文件
     * @param leftUnFocusResId
     *            (失去焦点时显示)当为0时表示不显示,其它则取资源文件
     * @param rightFocusResId
     *            当为0时表示不显示,其它则取资源文件
     * @param rightUnFocusResId
     *            当为0时表示不显示,其它则取资源文件
     */
    public void setDrawables(int leftFocusResId, int leftUnFocusResId, int rightFocusResId, int rightUnFocusResId) {
 
        if (leftFocusResId > 0) {
            imgLeftFoucus = context.getResources().getDrawable(leftFocusResId);
        }
 
        if (leftUnFocusResId > 0) {
            imgLeftUnFoucus = context.getResources().getDrawable(leftUnFocusResId);
        }
 
        if (rightFocusResId > 0) {
            imgRightFoucus = context.getResources().getDrawable(rightFocusResId);
        }
 
        if (rightUnFocusResId > 0) {
            imgRightUnFoucus = context.getResources().getDrawable(rightUnFocusResId);
        } else {
            imgRightUnFoucus = null;
        }
 
        setDrawable();
    }
 
    public void setFocus(boolean mFocus) {
        mbFocus = mFocus;
    }
 
    /**
     * 设置删除图片
     */
    private void setDrawable() {
        if (mbFocus) {
            if (length() == 0) {
                setCompoundDrawablesWithIntrinsicBounds(imgLeftFoucus, null, null, null);
            } else {
                setCompoundDrawablesWithIntrinsicBounds(imgLeftFoucus, null, imgRightFoucus, null);
            }
        } else {
            if (length() == 0) {
                setCompoundDrawablesWithIntrinsicBounds(imgLeftUnFoucus, null, null, null);
            } else {
                setCompoundDrawablesWithIntrinsicBounds(imgLeftUnFoucus, null, imgRightUnFoucus, null);
            }
        }
    }
 
    /**
     * event.getX() 获取相对应自身左上角的X坐标 event.getY() 获取相对应自身左上角的Y坐标 getWidth() 获取控件的宽度
     * getTotalPaddingRight() 获取删除图标左边缘到控件右边缘的距离 getPaddingRight() 获取删除图标右边缘到控件右边缘的距离 getWidth() -
     * getTotalPaddingRight() 计算删除图标左边缘到控件左边缘的距离 getWidth() - getPaddingRight() 计算删除图标右边缘到控件左边缘的距离
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgRightFoucus != null && event.getAction() == MotionEvent.ACTION_UP) {
            int x = (int) event.getX();
            // 判断触摸点是否在水平范围内
            boolean isInnerWidth = (x > (getWidth() - getTotalPaddingRight()))
                    && (x < (getWidth() - getPaddingRight()));
            // 获取删除图标的边界,返回一个Rect对象
            Rect rect = imgRightFoucus.getBounds();
            // 获取删除图标的高度
            int height = rect.height();
            int y = (int) event.getY();
            // 计算图标底部到控件底部的距离
            int distance = (getHeight() - height) / 2;
            // 判断触摸点是否在竖直范围内(可能会有点误差)
            // 触摸点的纵坐标在distance到(distance+图标自身的高度)之内,则视为点中删除图标
            boolean isInnerHeight = (y > distance) && (y < (distance + height));
            if (isInnerWidth && isInnerHeight && mbFocus) {
                setText("");
            }
 
        }
 
        return super.onTouchEvent(event);
    }
 
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
 
    public void onFocusChangeSelf(View v, boolean hasFocus) {
        mbFocus = hasFocus;
        setDrawable();
    }
 
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
 
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
 
        onFocusChangeSelf(this, focused);
    }
 
    /**
     * 设置最大输入长度,1个中文计两个英文,且自动屏蔽不规则字符
     * 
     * @param maxlen
     */
    public void setMaxLenth(int maxlen) {
        if (maxlen > 0) {
            mLimitedLen = maxlen;
        } else {
            mLimitedLen = 20;
        }
    }
 
    /**
     * 是否限制特殊字符输入
     * 
     * @param enbale
     */
    public void setUnregFilterEnbale(boolean enbale) {
        mbUnregFilter = enbale;
    }
 
    /**
     * 是否限中文及制特殊字符输入,常用于密码输入
     * 
     * @param enbale
     */
    public void setCHFilterEnbale(boolean enbale) {
        mbPWDFilter = enbale;
    }
 
    private int calcultateLength(CharSequence c) {
        int len = 0;
        for (int i = 0; i < c.length(); i++) {
            int tmp = (int) c.charAt(i);
            if (tmp > 0 && tmp < 127) {
                len++;
            } else {
                len += 2;
            }
        }
        return len;
    }
 
    private void filter(Editable s) {
        String str = s.toString();
        int indexStart = ClearAutoCompleteText.this.getSelectionStart();
 
        if (mbPWDFilter) {
            str = StringUtils.strPwdFilter(str);
            int iDelLen = s.length() - str.length();
            if (iDelLen > 0) {
                ClearAutoCompleteText.this.setText(str);
                if (indexStart - iDelLen >= 0 && indexStart - iDelLen <= str.length()) {
                    ClearAutoCompleteText.this.setSelection(indexStart - iDelLen);
                    indexStart -= iDelLen;
                }
            }
        } else if (mbUnregFilter) {
            str = StringUtils.strFilter(s.toString());
            int iDelLen = s.length() - str.length();
            if (iDelLen > 0) {
                ClearAutoCompleteText.this.setText(str);
                if (indexStart - iDelLen >= 0 && indexStart - iDelLen <= str.length()) {
                    ClearAutoCompleteText.this.setSelection(indexStart - iDelLen);
                    indexStart -= iDelLen;
                }
            }
        }
 
        limitLenght(str, indexStart);
    }
 
    private void limitLenght(String str, int indexStart) {
        if (mLimitedLen > 0) {
 
            boolean bFlag = false;
            if (calcultateLength(str) > mLimitedLen) {
                bFlag = true;
            }
            while (calcultateLength(str) > mLimitedLen) {
                if (indexStart > 0 && indexStart <= str.length()) {
                    str = str.substring(0, indexStart - 1) + str.substring(indexStart, str.length());
                } else {
                    str = str.substring(0, str.length() - 1);
                }
                indexStart = indexStart - 1;
            }
 
            if (bFlag) {
                ClearAutoCompleteText.this.setText(str);
                if (indexStart >= 0 && indexStart <= str.length()) {
                    ClearAutoCompleteText.this.setSelection(indexStart);
                }
            }
        }
    }
 
    public int getErrorTip() {
        return errorTip;
    }
 
    public void setErrorTip(int errorTip) {
        this.errorTip = errorTip;
    }
 
}