简体   繁体   English

关闭应用程序后如何保持选中单选按钮 - android studio

[英]How to keep a radio button checked after closing the application - android studio

I am new to coding in Android Studio and I just learned about Shared Prefrences.我是在 Android Studio 中编码的新手,我刚刚了解了共享偏好。 My teacher gave me an assignment to make a Radio Button Group (RadioGroup), that when you check one of the buttons, close the app, open it again, the button will remain checked.我的老师给我布置了一个单选按钮组(RadioGroup)的作业,当您选中其中一个按钮时,关闭应用程序,再次打开它,该按钮将保持选中状态。

Just like you would save an EditText answer with SharedPrefrences , I need to save a RadioButton with SharedPrefrences .就像您将使用SharedPrefrences保存EditText答案一样,我需要使用SharedPrefrences保存一个RadioButton

As I said I am new to this, so please keep the answer simple :)正如我所说,我是新手,所以请保持简单的答案:)

--SOLVED-- Thank you all for answering! --已解决--谢谢大家的回答! i found my solution thanks to your comments!感谢您的评论,我找到了我的解决方案! :D :D

Well first, you want to set listener to the RadioGroup like so:首先,您要像这样将侦听器设置为RadioGroup

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // save id into your SharedPreferences
        }
    });

And than you can restore it in onCreate() from shared preferences and call radioGroup.check(id);然后你可以在 onCreate() 中从共享首选项中恢复它并调用radioGroup.check(id); . .
So the code will look something like this:所以代码看起来像这样:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int checkedId = sharedPrefs.getInt("your key", 0);
radioGroup.check(checkedId);  

Please note that this code is simplified.请注意,此代码已简化。 You need to add checks and so on您需要添加检查等

Try This,尝试这个,

    RadioGroup radioGroup = findViewById(R.id.radio_gropup);
    final SharedPreferences preferences = getSharedPreferences("saved", 0);

    radioGroup.check(preferences.getInt("CheckedId", 0));

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("CheckedId", checkedId);
            editor.apply();
        }
    });

Here is how you are supposed to get the onCheckChangeListener on a radio group to get the selected radio button in it!这是您应该如何在单选组上获取onCheckChangeListener以获取其中的选定单选按钮!

NOTE!!!笔记!!! THERE SHOULD BE IDs GIVEN TO ALL INNER RADIO BUTTONS应该为所有内部无线电按钮提供 ID

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            RadioButton rb=(RadioButton)findViewById(checkedId);

            //textViewChoice.setText("You Selected " + rb.getText());
           // here the rb.getText() will give you the text and you can send it to your
           //sharedPreference with a new key value like you did with the editText values
        }
    });

And when your app starts you can check in your onCreate() by the sharefPreference if the value is saved then get the RadioButton and set its property isSelected as true!你的应用程序启动,你可以在你的onCreate()由sharefPreference检查值保存然后拿到单选按钮,并设置其属性isSelected为真! I hope that is simple enough!我希望这足够简单!

public class MainActivity extends AppCompatActivity {
   // this will be key for the key value pair
   public static final String BUTTON_STATE = "Button_State";
   // this is name of shared preferences file, must be same whenever accessing
   // the key value pair.
   public static final String MyPREFERENCES = "MyPrefs" ;

   SharedPreferences sharedpreferences;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // helper method to open up the file.
      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
      // grab the last saved state here on each activity start
      Boolean lastButtonState = sharedpreferences.getBoolean(BUTTON_STATE, false)
      RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
      // restore previous state
      rb.setChecked(lastButtonState);
      // set a listener
      rb.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // call this to enable editing of the shared preferences file
            // in the event of a change
            SharedPreferences.Editor editor = sharedpreferences.edit();
            Boolean isChecked = rb.isChecked();
            // use this to add the new state
            editor.putBoolean(BUTTON_STATE, isChecked);
            // save
            editor.apply();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保持单选按钮处于选中状态,直到在Android中的单选组中选中了另一个单选按钮? - How to keep the radio button checked till its other radio button is checked in a radio group in android? 如何在 android 工作室中保存选中的单选按钮的 state - How to save state of checked radio button in android studio 单选按钮仅在android studio中获取选中的收音机 - Radio button get only checked radio in android studio Android-如果选中了单选按钮,如何启用按钮? - Android - How to enable a button if a radio button is checked? 未选中 Android 单选按钮 - Android Radio Button Not Checked 如何在Android中选中正确的单选按钮? - How to get the right radio button checked in Android? 如何检查在android中选中了哪个单选按钮? - how to check which radio button is checked in android? 如何使用带有共享首选项的 android studio 在测验应用程序中保存单选按钮的检查状态 - How to save Checked Status of radio button in Quiz app using android studio with sharedpreferences Android - 选中单选按钮后自动更改LinearLayout可见性 - Android - Change LinearLayout visibility automatically after radio button is checked 如何在Android中的上一个按钮上设置选中的单选按钮? - How to set checked Radio Button on Previous Button in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM