没别的,只有代码自己看。。。
package zwp.mycalendar.widget; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.widget.EditText; /** * 自定义EditText * @author 周文鹏 * @date 2015-6-23 */ public class ZPEditText extends EditText { private List<EditHistory> undoHistories; // 撤销文字 private List<EditHistory> redoHistories; // 重做文字 private boolean activate = false; // 是否激活redo private boolean clear = true; // 是否清空redo private OnTextChangeListener listener; // 文字改变监听器 public ZPEditText(Context context) { this(context, null); } public ZPEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ZPEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化 */ private void init() { undoHistories = new ArrayList<EditHistory>(); redoHistories = new ArrayList<EditHistory>(); addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (listener != null) { listener.onTextChange(ZPEditText.this, s); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (activate) { redoHistories.add(new EditHistory(s.toString(), start, count, after)); activate = false; } else { undoHistories.add(new EditHistory(s.toString(), start, count, after)); if (clear) { redoHistories.clear(); } clear = true; } } @Override public void afterTextChanged(Editable s) { } }); } /** * 撤销 */ public void undo() { if (canUndo()) { activate = true; Editable editable = getEditableText(); EditHistory history = undoHistories.remove(undoHistories.size() - 1); if (history.count > 0) { editable.append(history.text.subSequence(history.start, history.start + history.count)); } if (history.after > 0) { editable.delete(history.start, history.start + history.after); } } } /** * 重做 */ public void redo() { if (canRedo()) { clear = false; Editable editable = getEditableText(); EditHistory history = redoHistories.remove(redoHistories.size() - 1); if (history.count > 0) { editable.append(history.text.subSequence(history.start, history.start + history.count)); } if (history.after > 0) { editable.delete(history.start, history.start + history.after); } } } /** * 是否可以撤销 */ public boolean canUndo() { if (undoHistories.size() > 0) { return true; } return false; } /** * 是否可以重做 */ public boolean canRedo() { if (redoHistories.size() > 0) { return true; } return false; } /** * 获取文字改变监听器 * @return */ public OnTextChangeListener getOnTextChangeListener() { return listener; } /** * 设置文字改变监听器 * @param listener */ public void setOnTextChangeListener(OnTextChangeListener listener) { this.listener = listener; } /** * 存储Edit操作 * @author 周文鹏 * @date 2015-6-24 */ private class EditHistory { public CharSequence text; public int start; public int count; public int after; public EditHistory(CharSequence text, int start, int count, int after) { this.text = text; this.start = start; this.count = count; this.after = after; } @Override public String toString() { return "EditHistory [text=" + text + ", start=" + start + ", count=" + count + ", after=" + after + "]"; } } /** * 文字改变监听器 * @author 周文鹏 * @date 2015-6-24 */ public interface OnTextChangeListener { void onTextChange(ZPEditText editText, CharSequence text); } }