weixin_46792691 2021-11-24 21:14 采纳率: 100%
浏览 121
已结题

android 按键不能响应(setEnabled相关)

初次添加软件功能,想实现文本撤回和重做功能。
大体思路:初始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();//实时统计字数

    }

img

尝试过修改初始状态,会直接退出,应该就是enabled的修改出了问题。

  • 写回答

1条回答 默认 最新

  • pumpkin的玄学 2021-11-24 21:35
    关注

    抛异常了吧 看一下控制台的错误日志 估计是你那里逻辑处理的问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月24日

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证