简体   繁体   English

为什么在与一个复选框关联的操作之后,两个复选框都被禁用?

[英]Why after action associated with one checkbox both checkboxes are are disabled?

everyone: I want to implement app that:大家:我想实现应用程序:

  1. Click on the checkbox点击复选框
  2. Move to new action移动到新动作
  3. Complete new action完成新动作
  4. Return to checkbox action返回复选框操作
  5. Set clicked checkbox checked and disabled将单击的复选框设置为选中并禁用

In my code when I try to implement it the second checkbox is also checked and disabled despite the fact that I haven't clicked it.在我的代码中,当我尝试实现它时,第二个复选框也被选中并禁用,尽管我没有点击它。

Please help: See my code below:请帮助:请参阅下面的代码:

    P2106 = findViewById(R.id.checkbox_p2106);
    MP2106 = findViewById(R.id.checkbox_mp2106);

    pref = PreferenceManager.getDefaultSharedPreferences(this);
    editor = pref.edit();

    CheckboxCare(P2106);
    CheckboxCare(MP2106);
}

public void CheckboxCare(@NonNull View v){
    switch (v.getId()){
        case R.id.checkbox_p2106:
        case R.id.checkbox_mp2106:
            if(pref.contains("checked") && pref.getBoolean("checked", false) == true){
                setChecked((CheckBox) v);
                disableCheckbox((CheckBox) v);
            }else{
                setNotChecked((CheckBox) v);
            }
            checkboxListener((CheckBox) v);
            break;
    }
}

public void checkboxListener(@NonNull CheckBox checkBox){
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(checkBox.isChecked()){
                editor.putBoolean("checked", true);
                editor.apply();
                Intent move = new Intent(GeneralRoute.this, ScanTest.class);
                startActivity(move);
            }else{
                editor.putBoolean("checked", false);
                editor.apply();
            }
        }
    });
}

public void setChecked(@NonNull CheckBox checkBox){
    checkBox.setChecked(true);
}
public void setNotChecked(@NonNull CheckBox checkBox){
    checkBox.setChecked(false);
}
public void disableCheckbox(@NonNull CheckBox checkBox){
    checkBox.setEnabled(false);
}

Create object for each check box send on that function call based on your requirement根据您的要求,为发送该 function 调用的每个复选框创建 object

public void CheckboxCare(@NonNull View v){
switch (v.getId()){
    case R.id.checkbox_p2106:
                     {
                     // write code here
                     }
    case R.id.checkbox_mp2106:
        if(pref.contains("checked") && pref.getBoolean("checked", false) == true){
            setChecked(checkbox_p2106);// enable one
            disableCheckbox(checkbox_p2106);// disable second
        }else{
            setNotChecked((CheckBox) v);
        }
        checkboxListener((CheckBox) v);
        break;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么CheckBox Preference的摘要显示为禁用状态,为什么单击它后CheckBox的值没有变化? - Why CheckBox Preference's summary is appearing like it is disabled & why checkBox's value is not changing after clicking it? 如果选中前一个复选框(首先禁用其他复选框),如何在回收站视图中启用其他复选框? - How to enable other checkboxes in recycler view if previous checkbox is checked (other checkboxes are disabled at first)? javafx-选中表格视图中的复选框时,选中行中的其他复选框 - javafx - checkboxes in tableview when one is selected others in row are disabled 检查所有复选框,当在android中单击一个复选框时? - check all checkboxes, when one checkbox is clicked in android? Android:选中一个CheckBox会检查同一行中的所有CheckBox - Android: Checking one CheckBox checks all CheckBoxes in the same row 为什么将ChangeListener与CheckBox相关联,并且在TreeCell中多次运行 - Why is the ChangeListener associated to a CheckBox running multiple times in the TreeCell 已禁用添加到面板的已禁用复选框 - Disabled checkbox added to the panel is not disabled 如果两个复选框都被选中怎么办? - What if both the Checkboxes are selected? 复选框显示为已禁用 - Checkbox shows up as disabled 禁用复选框(如果选中) - Disabled checkbox if any is checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM