19
19
import android .support .annotation .NonNull ;
20
20
import android .text .Editable ;
21
21
import android .text .TextWatcher ;
22
+ import android .util .Log ;
22
23
import android .widget .EditText ;
23
24
24
25
import java .util .Stack ;
30
31
public class PerformEdit {
31
32
//操作序号(一次编辑可能对应多个操作,如替换文字,就是删除+插入)
32
33
int index ;
34
+ //撤销栈
33
35
Stack <Action > history = new Stack <>();
36
+ //恢复栈
34
37
Stack <Action > historyBack = new Stack <>();
35
38
36
39
private Editable editable ;
40
+ private EditText editText ;
37
41
//自动操作标志,防止重复回调,导致无限撤销
38
42
private boolean flag = false ;
39
43
40
44
public PerformEdit (@ NonNull EditText editText ) {
41
45
CheckNull (editText , "EditText不能为空" );
42
- editable = editText .getText ();
46
+ this .editable = editText .getText ();
47
+ this .editText = editText ;
43
48
editText .addTextChangedListener (new Watcher ());
44
49
}
45
50
@@ -74,10 +79,12 @@ public final void undo() {
74
79
historyBack .push (action );
75
80
if (action .isAdd ) {
76
81
//撤销添加
77
- editable .delete (action .cursor , action .cursor + action .actionTarget .length ());
82
+ editable .delete (action .startCursor , action .startCursor + action .actionTarget .length ());
83
+ editText .setSelection (action .startCursor , action .startCursor );
78
84
} else {
79
85
//插销删除
80
- editable .insert (action .cursor , action .actionTarget );
86
+ editable .insert (action .startCursor , action .actionTarget );
87
+ editText .setSelection (action .startCursor , action .endCursor );
81
88
}
82
89
//释放操作
83
90
flag = false ;
@@ -98,10 +105,12 @@ public final void redo() {
98
105
history .push (action );
99
106
if (action .isAdd ) {
100
107
//恢复添加
101
- editable .insert (action .cursor , action .actionTarget );
108
+ editable .insert (action .startCursor , action .actionTarget );
109
+ editText .setSelection (action .startCursor , action .endCursor );
102
110
} else {
103
111
//恢复删除
104
- editable .delete (action .cursor , action .cursor + action .actionTarget .length ());
112
+ editable .delete (action .startCursor , action .startCursor + action .actionTarget .length ());
113
+ editText .setSelection (action .startCursor , action .startCursor );
105
114
}
106
115
flag = false ;
107
116
//判断是否是下一个动作是否和本动作是同一个操作
@@ -127,7 +136,7 @@ private class Watcher implements TextWatcher {
127
136
*
128
137
* @param s the s
129
138
* @param start the start 起始光标
130
- * @param count the count 选择数量
139
+ * @param count the endCursor 选择数量
131
140
* @param after the after 替换增加的文字数
132
141
*/
133
142
@ Override
@@ -136,9 +145,18 @@ public final void beforeTextChanged(CharSequence s, int start, int count, int af
136
145
int end = start + count ;
137
146
if (end > start && end <= s .length ()) {
138
147
CharSequence charSequence = s .subSequence (start , end );
139
- //发生文字变化
148
+ //删除了文字
140
149
if (charSequence .length () > 0 ) {
141
150
Action action = new Action (charSequence , start , false );
151
+ if (count > 1 ) {
152
+ //如果一次超过一个字符,说名用户选择了,然后替换或者删除操作
153
+ action .setSelectCount (count );
154
+ }else if (count ==1 &&count ==after ){
155
+ //一个字符替换
156
+ action .setSelectCount (count );
157
+ }
158
+ //还有一种情况:选择一个字符,然后删除(暂时没有考虑这种情况)
159
+
142
160
history .push (action );
143
161
historyBack .clear ();
144
162
action .setIndex (++index );
@@ -152,17 +170,18 @@ public final void beforeTextChanged(CharSequence s, int start, int count, int af
152
170
* @param s the s
153
171
* @param start the start 起始光标
154
172
* @param before the before 选择数量
155
- * @param count the count 添加的数量
173
+ * @param count the endCursor 添加的数量
156
174
*/
157
175
@ Override
158
176
public final void onTextChanged (CharSequence s , int start , int before , int count ) {
159
177
if (flag ) return ;
160
178
int end = start + count ;
161
179
if (end > start ) {
162
180
CharSequence charSequence = s .subSequence (start , end );
163
- //发生文字变化
181
+ //添加文字
164
182
if (charSequence .length () > 0 ) {
165
183
Action action = new Action (charSequence , start , true );
184
+
166
185
history .push (action );
167
186
historyBack .clear ();
168
187
if (before > 0 ) {
@@ -191,18 +210,25 @@ private class Action {
191
210
/** 改变字符. */
192
211
CharSequence actionTarget ;
193
212
/** 光标位置. */
194
- int cursor ;
213
+ int startCursor ;
214
+ int endCursor ;
195
215
/** 标志增加操作. */
196
216
boolean isAdd ;
197
217
/** 操作序号. */
198
218
int index ;
199
219
200
- public Action (CharSequence actionTag , int cursor , boolean add ) {
220
+
221
+ public Action (CharSequence actionTag , int startCursor , boolean add ) {
201
222
this .actionTarget = actionTag ;
202
- this .cursor = cursor ;
223
+ this .startCursor = startCursor ;
224
+ this .endCursor = startCursor ;
203
225
this .isAdd = add ;
204
226
}
205
227
228
+ public void setSelectCount (int count ) {
229
+ this .endCursor = endCursor + count ;
230
+ }
231
+
206
232
public void setIndex (int index ) {
207
233
this .index = index ;
208
234
}
0 commit comments