简体   繁体   中英

how to check radio buttons correctly

Hi how can i check radio buttons correctly?

wrote this but its wrong and always " dancer" saves

   int career = userSample.getCareer();

    if (career == user_sample.gamer) {
        profile_gamer_button.setChecked(true);

    } else if (career == user_sample.dancer) {
        profile_dancer_button.setChecked(true);

    } else if (career == user_sample.soccer_player) {
        profile_soccer_player_button.setChecked(true);
    }

    profile_gamer_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            userSample.setCareer(user_sample.gamer);
        }


    });

    profile_dancer_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            userSample.setCareer(user_sample.dancer);
        }
    });

    profile_soccer_player_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            userSample.setCareer(user_sample.soccer_player);
        }
    });

    profile_save_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            user_sharedpref_manager.save_user(userSample);
            Toast.makeText(user_profile.this, "clicked", Toast.LENGTH_LONG).show();
        }
    });

thanks

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Check the documentation:

onCheckedChanged(CompoundButton buttonView, boolean isChecked). Called when the checked state of a compound button has changed.Called

Need to save isChecked parameter when callback triggered.

More: https://developer.android.com/reference/android/widget/CompoundButton.html#setOnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener)

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