【发布时间】:2014-01-02 19:56:47
【问题描述】:
我正在尝试设置一个配置文件,其中用户通过代表每个的复选框选择男性或女性。然后我尝试使用 SharedPreference 对象保存这些数据。为两个复选框添加了 onClickListener,如果选中,则布尔值设置为 true。
但是无论我尝试了什么(我已经尝试了 4 个小时!!!!!!)当我刷新活动时,两个框都被选中并且两个布尔值都是真的!任何想法都会很棒。
// check box initialisation male and female
male = (CheckBox) this.findViewById(R.id.cb_MalePrfofile);
male.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when male is checked
if (((CheckBox) v).isChecked()) {
sex = "Male";
//malePref=true;
//femalePref=false;
female.setChecked(false);
Toast.makeText(getBaseContext(), "malePref=true", Toast.LENGTH_SHORT).show();
}
}
});// end on click for male
// if female check box is checked
female = (CheckBox) this.findViewById(R.id.cb_FemalePrfofile);
female.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when male is checked
if (((CheckBox) v).isChecked()) {
sex = "Female";
//femalePref=true;
//malePref=false;
male.setChecked(false);
Toast.makeText(getBaseContext(), "feamlePref=true", Toast.LENGTH_SHORT).show();
}
}// end onClick/if
});// end on click call for male
}// end intilize widgets
设置 SharedPref 编辑器:
//Checkboxes
if(male.isChecked()){
//boolean maleValue=true;
editor.putBoolean("maleValue", true);
}else if(female.isChecked()){
//boolean femaleValue=true;
editor.putBoolean("femaleValue", true);
}//end else
onCreate 方法根据保存的偏好设置数据:
male.setChecked(prefs.getBoolean("maleValue", false));
female.setChecked(prefs.getBoolean("femaleValue", false));
XML:
<CheckBox
android:id="@+id/cb_MalePrfofile"
android:text="Male"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></CheckBox>
<CheckBox
android:id="@+id/cb_FemalePrfofile"
android:layout_width="wrap_content"
android:text="Female"
android:typeface="serif"
android:layout_height="wrap_content"></CheckBox>
回答cmets: 我已经有了 editor.commit() 并且所有其他来自微调器的数据、编辑文本等都保存得很好。
还尝试添加 Radiogoup,但无论我选择哪个,默认情况下都会选择男性加班活动重新加载。很抱歉这里的代码很长,但我将 onCreate 方法发布为可能在这里遗漏了一些东西:
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.profile);
intilizeWidgets();
//load teh sahred prefs object
prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
//SharedPreferences.Editor editor = prefs.edit();
//save teh sored values in the Edittexts
firstName.setText(prefs.getString("firstName", null));
lastName.setText(prefs.getString("lastName", null));
insuranceNo.setText(prefs.getString("insuranceNumber", null));
padiNumber.setText(prefs.getString("padiNum", null));
aboutMe.setText(prefs.getString("aboutMe", null));
boolean maleBoo = prefs.getBoolean("maleValue", false);
Toast.makeText(getBaseContext(), "Male checked is " + maleBoo, Toast.LENGTH_SHORT).show();
male.setChecked(prefs.getBoolean("maleValue", false));
female.setChecked(prefs.getBoolean("femaleValue", false));
boolean femaleBoo = prefs.getBoolean("femaleValue", false);
Toast.makeText(getBaseContext(), "FeMale checked is " + femaleBoo, Toast.LENGTH_SHORT).show();
certLevel.setSelection(prefs.getInt("certLevel", 0));
yearsExperince.setSelection(prefs.getInt("yearsExp", 0));
//radiobuttons
isMaleButton.setChecked(prefs.getBoolean("maleButton", false));
isFemaleButton.setChecked(prefs.getBoolean("femaleButton", false));
}// end onctreate
当点击保存按钮时,我得到共享首选项对象和编辑器
public void onClick(View arg0) {
//get the sahred pref object and its editor to accept values
this.prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if(isMaleButton.isChecked()){
editor.putBoolean("maleButton", true);
}else if(isFemaleButton.isChecked()){
editor.putBoolean("femaleButton", true);
}
【问题讨论】:
-
你的
editor.apply()s 在哪里?另外,请发你的onCreate();
标签: android checkbox sharedpreferences