mardi 9 mai 2017

Event of multiple checkboxes using onCheckedChanged

I have a problem with login checkboxes

First, I`m going to make 3 checkboxes and created change listener for each

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

private CheckBox main_id_saver;
private CheckBox main_pw_saver;
private CheckBox main_auto_login;

and allocate ID of wigets for each too in onCreate(xml file is skipped)

main_id_saver = (CheckBox) findViewById(R.id.main_id_saver);
main_pw_saver = (CheckBox) findViewById(R.id.main_pw_saver);
main_auto_login = (CheckBox) findViewById(R.id.main_auto_login);

main_id_saver.setOnCheckedChangeListener(this);
main_pw_saver.setOnCheckedChangeListener(this);
main_auto_login.setOnCheckedChangeListener(this);

and I want to set a click event for checkboxes which satisfies below

  1. If ID, PW checkboxes is checked, then auto login checkboxes are checked automatically

  2. cannot check PW checkbox only

  3. If auto login is checked, then ID, PW are checked automatically

  4. If any checkbox is clicked in status of all checkboxes are checked, then all checkboxes are unchecked

I made some tries and below is that

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (main_auto_login.isChecked()){
        if(!main_id_saver.isChecked() || !main_pw_saver.isChecked()) {
            main_id_saver.setChecked(true);
            main_pw_saver.setChecked(true);
            return;
        } else if (main_id_saver.isChecked()&&main_pw_saver.isChecked()&&main_auto_login.isChecked()){
            main_auto_login.setChecked(false);
            main_id_saver.setChecked(false);
            main_pw_saver.setChecked(false);
            return;
        }
    } else if(main_pw_saver.isChecked()){
        if(main_id_saver.isChecked()){
            main_auto_login.setChecked(true);
            return;
        } else if(!main_id_saver.isChecked()){
            Toast.makeText(MainActivity.this, "CANNOT REMEMBER PASSWORD BUT FOR ID",Toast.LENGTH_SHORT).show();
            main_pw_saver.setChecked(false);
            return;
        }
    }
    return;
}

but it doesn`t work as I have thought

what is the best code for this problem?

In my arrogant opinion, making onChangedLinster event separately is the best answer but I cannot make it...




Aucun commentaire:

Enregistrer un commentaire