我可以在保存的偏好设置中保存字符串,但无法保存单选按钮。
public class PersonalDetailsf extends Activity {
private SharedPreferences sharedPreferences;
private RadioGroup radioGroup;
private RadioButton radioSexButton;
private RadioButton rdoMale;
这是我的创建:
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后面的按钮,我正在使用单选按钮并保存字符串:
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);
答案 0 :(得分:2)
创建保存和加载方法:
保存方法
public void loadRadioButtons(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false));
rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));
}
加载方法
saveRadioButtons()
如此保存您的按钮状态,只需拨打loadRadioButtons()
并重新加载您的按钮状态,只需在代码中的某个位置调用onCreate()
,例如QTreeWidget* tree = new QTreeWidget(this);
QTreeWidgetItem* itemMen = new QTreeWidgetItem({"Men"});
QTreeWidgetItem* itemMark = new QTreeWidgetItem({"Mark"});
QTreeWidgetItem* itemSteve = new QTreeWidgetItem({"Steve"});
tree->addTopLevelItem(itemMen);
itemMen->addChild(itemMark);
itemMen->addChild(itemSteve);
希望这会帮助你
答案 1 :(得分:1)
在onClick()
内做
sharedPreferences.edit().putBoolean("bntChecked", rdoMale.isChecked()).apply();
"btnChecked"
将是您使用sharedPreferences获取单选按钮状态的关键