我是Android Studio编码方面的新手,我刚刚了解了共享偏好。 我的老师给我分配了一个单选按钮组(RadioGroup),当您选中其中一个按钮时,关闭应用程序,然后再次打开它,该按钮将保持选中状态。
就像您用EditText
保存一个SharedPrefrences
答案一样,我需要用RadioButton
保存一个SharedPrefrences
。
正如我所说的,我是新手,所以请保持简单的答案:)
-已解决-谢谢大家的回答!感谢您的评论,我找到了解决方案! :D
答案 0 :(得分:0)
这是方法,您应该在单选组上获取 onCheckChangeListener ,以在其中获得所选的单选按钮!
注意!!!应该为所有内部无线电按钮提供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
}
});
然后,当您的应用启动时,您可以通过sharefPreference检入onCreate(),是否保存了该值,然后获取RadioButton并将其属性isSelected设置为true!我希望这很简单!
答案 1 :(得分:0)
首先,您要像这样将监听器设置为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
}
});
然后您可以从共享的首选项中的onCreate()中恢复它,并调用radioGroup.check(id);
。
因此,代码将如下所示:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int checkedId = sharedPrefs.getInt("your key", 0);
radioGroup.check(checkedId);
请注意,此代码已简化。您需要添加支票等等
答案 2 :(得分:0)
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();
}
});
}
}
答案 3 :(得分:0)
尝试一下
RadioGroup 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();
}
});