[英]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.