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

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日

悬赏问题

  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 求解四个变换矩阵中的未知数θ
  • ¥20 Intellij实现web登录界面
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现
  • ¥15 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?