简体   繁体   中英

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

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;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM