如何正确检查单选按钮

时间:2018-10-29 17:21:41

标签: java android

嗨,我如何正确检查单选按钮?

写了这个,但它的错误并总是“舞者”保存

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

谢谢

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

1 个答案:

答案 0 :(得分:0)

查看文档:

  

onCheckedChanged(CompoundButton buttonView,boolean isChecked)。   当复合按钮的选中状态更改时调用。调用

触发回调时需要保存isChecked参数。

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