簡體   English   中英

如何正確檢查單選按鈕

[英]how to check radio buttons correctly

嗨,我如何正確檢查單選按鈕?

寫了這個,但它是錯誤的,總是“舞者”保存

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

謝謝

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

檢查文檔:

onCheckedChanged(CompoundButton buttonView, boolean isChecked)。 當復合按鈕的選中狀態改變時調用。調用

觸發回調時需要保存isChecked參數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM