初次添加软件功能,想实现文本撤回和重做功能。
大体思路:初始button 的enabled定义为false,后面根据判断条件借助setEnabled恢复按键。
出现问题:在写完全部代码后,发现在满足条件下,按键依旧灰色。
<Button
android:id="@+id/button_main_undo"
android:text="@string/undo"
android:onClick="widget_undo"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_main_redo"
android:text="@string/redo"
android:onClick="widget_redo"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public class StringRecorder {
int currentCursorPosition;
List<EditRecord> editRecordUndoList = new ArrayList<>();
List<EditRecord> editRecordRedoList = new ArrayList<>();
public void insertEdit(boolean p_isInsert, String p_newContent, String p_originalContent, int p_startPosition){
editRecordUndoList.add(new EditRecord(p_isInsert,p_newContent,p_originalContent,p_startPosition));
editRecordRedoList.clear();
}
public int currentCursorPosition(){
return currentCursorPosition;
}
public void clearRecord(){
editRecordUndoList.clear();
editRecordRedoList.clear();
}
public String undo(String p_currentContent){
String _result;
if(!canUndo()){
return p_currentContent;
}
EditRecord _movingEditRecord = editRecordUndoList.get(editRecordUndoList.size() - 1);
currentCursorPosition = _movingEditRecord.editStartPosition;
if(_movingEditRecord.isInsert){
_result = p_currentContent.substring(0,_movingEditRecord.editStartPosition);
if(_movingEditRecord.originalContent.length() > 0) {
_result = _result + _movingEditRecord.originalContent;
}
_result = _result + p_currentContent.substring(_movingEditRecord.editStartPosition + _movingEditRecord.newContent.length());
}
else{
currentCursorPosition = currentCursorPosition + 1;
_result = p_currentContent.substring(0,_movingEditRecord.editStartPosition);
_result = _result + _movingEditRecord.newContent;
_result = _result + p_currentContent.substring(_movingEditRecord.editStartPosition);
}
editRecordUndoList.remove(editRecordUndoList.size()-1);
editRecordRedoList.add(_movingEditRecord);
return _result;
}
public String redo(String p_currentContent){
String _result;
if(!canRedo()){
return p_currentContent;
}
EditRecord _movingEditRecord = editRecordRedoList.get(editRecordRedoList.size() - 1);
currentCursorPosition = _movingEditRecord.editStartPosition;
if(_movingEditRecord.isInsert){
_result = p_currentContent.substring(0,_movingEditRecord.editStartPosition);
_result = _result + _movingEditRecord.newContent;
currentCursorPosition = currentCursorPosition + _movingEditRecord.newContent.length();
if(_movingEditRecord.originalContent.length() > 0) {
_result = _result + p_currentContent.substring(_movingEditRecord.editStartPosition + _movingEditRecord.originalContent.length());
}
else {
_result = _result + p_currentContent.substring(_movingEditRecord.editStartPosition);
}
}
else{
_result = p_currentContent.substring(0,_movingEditRecord.editStartPosition);
_result = _result + p_currentContent.substring(_movingEditRecord.editStartPosition+_movingEditRecord.newContent.length());
}
editRecordRedoList.remove(editRecordRedoList.size()-1);
editRecordUndoList.add(_movingEditRecord);
return _result;
}
public boolean canUndo(){
return editRecordUndoList.size() > 0;
}
public boolean canRedo(){
return editRecordRedoList.size() > 0;
}
class EditRecord{
boolean isInsert;
String newContent;
String originalContent;
int editStartPosition;
public EditRecord(boolean p_insert,String p_newContent, String p_originalContent, int p_startPosition){
isInsert = p_insert;
newContent = p_newContent;
originalContent = p_originalContent;
editStartPosition = p_startPosition;
}
}
}
//EditText添加事件响应器
public EditText editText_content;
StringRecorder stringRecorder = new StringRecorder();
boolean activeTextWatcher = true;
private void setWidget(){
editText_content.addTextChangedListener(new TextWatcher() {
String _newContent;
boolean _isInsert;
int _position;
String _originalContent;
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(!activeTextWatcher){
return;
}
_position = i;
if(i2 < 1){
_isInsert = false;
_newContent = charSequence.subSequence(i,i+i1).toString();
}
else{
_isInsert = true;
if(i1 > 0){
_originalContent = charSequence.subSequence(i,i+i1).toString();
}
else{
_originalContent = "";
}
}
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Button button_undo = findViewById(R.id.button_main_undo);
Button button_redo = findViewById(R.id.button_main_redo);
if(!activeTextWatcher){
return;
}
if(_isInsert){
_newContent = charSequence.subSequence(i,i+i2).toString();
}
stringRecorder.insertEdit(_isInsert,_newContent,_originalContent,_position);
if(stringRecorder.canUndo()){
button_undo.setEnabled(true);
}
if(stringRecorder.canRedo()){
button_redo.setEnabled(true);
}
else{
button_redo.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
// Button绑定方法
public void clicked(View e){
switch(e.getId()){//R是gen文件中自动生成的
case R.id.button_main_redo:
Toast.makeText(this, "按键一被按下", 0).show();
System.out.println("按键1被按下");
break;
case R.id.button_main_undo:
Toast.makeText(this, "按键二被按下", 0).show();
System.out.println("按键2被按下");
break;
}
}
// 撤销
public void widget_undo(View p_view){
Button button_undo = findViewById(R.id.button_main_undo);
Button button_redo = findViewById(R.id.button_main_redo);
activeTextWatcher = false;
editText_content.setText(stringRecorder.undo(editText_content.getText().toString()));
editText_content.setSelection(stringRecorder.currentCursorPosition());
activeTextWatcher = true;
if(!stringRecorder.canUndo()){
button_undo.setEnabled(false);
}
if(stringRecorder.canRedo()){
button_redo.setEnabled(true);
}
}
// 重做
public void widget_redo(View p_view){
Button button_undo = findViewById(R.id.button_main_undo);
Button button_redo = findViewById(R.id.button_main_redo);
activeTextWatcher = false;
editText_content.setText(stringRecorder.redo(editText_content.getText().toString()));
editText_content.setSelection(stringRecorder.currentCursorPosition());
activeTextWatcher = true;
if(!stringRecorder.canRedo()){
button_redo.setEnabled(false);
}
if(stringRecorder.canUndo()){
button_undo.setEnabled(true);
}
}
下面是oncreat部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.note_edit);
if (savedInstanceState == null && !initActivityState(getIntent())) {
finish();
return;
}
initResources();
count();//实时统计字数
}
尝试过修改初始状态,会直接退出,应该就是enabled的修改出了问题。