Home > Back-end >  Working with RadioButtons and Buttons in Android Studio using JAVA
Working with RadioButtons and Buttons in Android Studio using JAVA

Time:10-05

So I made seven RadioButtons. There are two groups. First three and Last four! I want to enable "submit" button if both groups are checked. But the problem is that it enables the button even if I check one of the radio buttons. So basically it ignores and(&&) statment. Here's my code:

public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    boolean checkeds = ((RadioButton) view).isChecked();
    
    if(!thirty.isChecked() || !fourty.isChecked() || !fifty.isChecked() && !geo.isChecked() || !eng.isChecked() || !country.isChecked() || !movi.isChecked()){
        submit.setEnabled(true);
    }else{
        submit.setEnabled(false);
    }

    switch (view.getId()) {
        case R.id.thirty:
            if (checked) {
                scorelimit = 3;
                fourty.setChecked(false);
                fifty.setChecked(false);
            }break;
        case R.id.fourty:
            if (checked) {
                scorelimit = 4;
                thirty.setChecked(false);
                fifty.setChecked(false);
            }break;
        case R.id.fifty:
            if (checked) {
                scorelimit = 5;
                thirty.setChecked(false);
                fourty.setChecked(false);
            }break;
    }
    switch (view.getId()) {
        case R.id.geo:
            if (checkeds) {
                gamemode = 3;
                eng.setChecked(false);
                country.setChecked(false);
                movi.setChecked(false);
            }break;
        case R.id.eng:
            if (checkeds) {
                gamemode = 4;
                geo.setChecked(false);
                country.setChecked(false);
                movi.setChecked(false);
            }break;
        case R.id.country:
            if (checkeds) {
                gamemode = 5;
                geo.setChecked(false);
                eng.setChecked(false);
                movi.setChecked(false);
            }break;
        case R.id.movi:
            if (checkeds) {
                gamemode = 6;
                geo.setChecked(false);
                eng.setChecked(false);
                country.setChecked(false);
            }break;
    }
}

CodePudding user response:

since AND is executed before OR, it basically only takes AND of fifty.isChecked()&&geo.isChecked() and OR of the rest with the result of the previous AND, you should add brackets, and if I understand correctly you should be enabling the submit if one of number group {thirty, forty, fifty} and one of category group {eng, geo, movi, country} is checked, so to acheive this I removed the NOT that was put before each of them, it should look like this in the end.

if((thirty.isChecked() || fourty.isChecked() || fifty.isChecked()) && (geo.isChecked() || eng.isChecked() || country.isChecked() || movi.isChecked()))

now it should mean if((one of first group is checked) and (one of the second group is checked))then enable the submit button

  • Related