如何将单选按钮状态保存到已保存/共享的首选项? [英] How to save radio button status to saved/shared preferences?
本文介绍了如何将单选按钮状态保存到已保存/共享的首选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在保存的首选项中保存字符串,但是在保存单选按钮时遇到困难.
I am able to save Strings in my saved preferences but having difficulty saving radio buttons.
public class PersonalDetailsf extends Activity {
private SharedPreferences sharedPreferences;
private RadioGroup radioGroup;
private RadioButton radioSexButton;
private RadioButton rdoMale;
这是我在创建上的内容:
Here is my on Create:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = loadSavedPreference("name");
strAge = loadSavedPreference("strAge");
strHeight = loadSavedPreference("strHeight");
strWeight = loadSavedPreference("strWeight");
etName.setText(name);
etAge.setText(strAge);
etHeight.setText(strHeight);
etWeight.setText(strWeight);
这在按钮后面的onCLick中,我在其中使用单选按钮并保存字符串:
This is in my onCLick behind a button, where I'm using the radio button and saving the strings:
Name = etName.getText().toString();
age = (int) Double.parseDouble(etAge.getText().toString());
height = (int) Double.parseDouble(etHeight.getText().toString());
weight = (int) Double.parseDouble(etWeight.getText().toString());
int selectedId = radioGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
rdoMale = (RadioButton) findViewById(R.id.rdoMale);
if(rdoMale.isChecked())
{
BMR = 10 * weight + 6.25 * height - 5 * age + 5;
}
else
{
BMR = 10 * weight + 6.25 * height - 5 * age -161;
}
//Save Preferences
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = etName.getText().toString();
savePreference("name",name);
strAge = etAge.getText().toString();
savePreference("strAge",strAge);
strHeight = etHeight.getText().toString();
savePreference("strHeight",strHeight);
strWeight = etWeight.getText().toString();
savePreference("strWeight",strWeight);
推荐答案
在 onClick()
内部执行
sharedPreferences.edit().putBoolean("bntChecked", rdoMale.isChecked()).apply();
"btnChecked"
将是您使用sharedPreferences获取单选按钮状态的键
And "btnChecked"
will be the key that you will use to get the status of your radio button using sharedPreferences
这篇关于如何将单选按钮状态保存到已保存/共享的首选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文