doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
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
package com.example.datalibrary.utils;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
 
/**
 *  * 在间隔处加一个标识符
 *  * 
 *  * @author wzw
 *  *
 *  
 */
@SuppressLint("AppCompatCustomView")
public class IntervalAddTagEditText extends EditText {
 
    /**
     *  * 位数
     *  
     */
    private int unit = 4;
    /**
     *  * 标识符
     *  
     */
    private String tag = "-";
 
    public IntervalAddTagEditText(Context context) {
        super(context);
        init();
    }
 
    public IntervalAddTagEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    void init() {
        addTextChangedListener(new FormatTextWatcher());
    }
 
    class FormatTextWatcher implements TextWatcher {
 
        int beforeTextLength = 0;
        int afterTextLength = 0;
        /**
         *  * 记录光标的位置
         *  
         */
        int location = 0;
        /**
         *  * 是否更换中
         *  
         */
        boolean isChanging = false;
 
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            beforeTextLength = s.length();
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
 
        }
 
        @Override
        public void
 
        afterTextChanged(Editable s) {
            afterTextLength = s.length();
            if (isChanging) {
                return;
            }
            /**
             *  * 字符增加
             *  
             */
            if (beforeTextLength < afterTextLength) {
                location = getSelectionEnd();
                setFormatText(s.toString());
                if (location % (unit + 1) == 0) {
                    setSelection(getLocation(location + 1));
                } else {
                    setSelection(getLocation(location));
                }
                /**
                 *  * 字符减少
                 *  
                 */
            } else if (beforeTextLength > afterTextLength) {
                location = getSelectionEnd();
                setFormatText(s.toString());
                if (location % (unit + 1) == 0) {
                    setSelection(getLocation(location - 1));
                } else {
                    setSelection(getLocation(location));
                }
            }
        }
 
        private int getLocation(int location) {
            if (location < 0) {
                return 0;
            }
            if (location > afterTextLength) {
                return afterTextLength;
            }
            return location;
        }
 
        private void setFormatText(String str) {
            isChanging = true;
            setText(addTag(str));
            isChanging = false;
        }
 
        /**
         *  * 加上标识符
         *  * @param str
         *  * @return
         *  
         */
        private String addTag(String str) {
            str = replaceTag(str);
            StringBuilder sb = new StringBuilder();
            int index = 0;
            int strLength = str.length();
            while (index < strLength) {
                int increment = index + unit;
                sb.append(str.subSequence(index, index = (increment > strLength) ? strLength : increment));
                if (increment < strLength) {
                    sb.append(tag);
                }
            }
            return sb.toString();
        }
    }
 
    /**
     *  * 清除标识符
     *  * @param str
     *  * @return
     *  
     */
    public String replaceTag(String str) {
        if (str.indexOf(tag) != -1) {
            str = str.replace(tag, "");
        }
        return str;
    }
 
    @Override
    public String toString() {
        return replaceTag(getText().toString());
    }
}