我有一个带有7个复选框,1个文本字段和一个保存按钮的应用程序.....我希望用户能够根据需要选择任意数量的复选框,并在文本字段中键入并单击保存。 ...然后当他们再次打开应用程序时,应检查正确的复选框,并且他们键入的文本应该在文本框中....
我几乎得到它的工作,但它似乎只是一次保存一个复选框或根本没有,我似乎无法弄清楚我做错了什么...我已经阅读了所有的线程和帖子,似乎不能要做对了......
任何帮助将不胜感激
这是我的代码
package com.rapeventplannerchecklist.app.rapeventplannerchecklist;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
CheckBox checkBox5;
CheckBox checkBox6;
CheckBox checkBox7;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
checkBox6 = (CheckBox) findViewById(R.id.checkBox6);
checkBox7 = (CheckBox) findViewById(R.id.checkBox7);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
String name = sharedPreferences.getString("storedName", "YourName");
if (checkBoxValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
boolean checkBox2Value = sharedPreferences.getBoolean("CheckBox_Value2", false);
if (checkBox2Value) {
checkBox2.setChecked(true);
} else {
checkBox2.setChecked(false);
}
boolean checkBox3Value = sharedPreferences.getBoolean("CheckBox_Value3", false);
if (checkBox3Value) {
checkBox3.setChecked(true);
} else {
checkBox3.setChecked(false);
}
boolean checkBox4Value = sharedPreferences.getBoolean("CheckBox_Value4", false);
if (checkBox4Value) {
checkBox4.setChecked(true);
} else {
checkBox4.setChecked(false);
}
boolean checkBox5Value = sharedPreferences.getBoolean("CheckBox_Value5", false);
if (checkBox5Value) {
checkBox5.setChecked(true);
} else {
checkBox5.setChecked(false);
}
boolean checkBox6Value = sharedPreferences.getBoolean("CheckBox_Value6", false);
if (checkBox6Value) {
checkBox6.setChecked(true);
} else {
checkBox6.setChecked(false);
}
boolean checkBox7Value = sharedPreferences.getBoolean("CheckBox_Value7", false);
if (checkBox7Value) {
checkBox7.setChecked(true);
} else {
checkBox7.setChecked(false);
}
editText.setText(name);
}
private void savePreferences(String key, boolean value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CheckBox_Value", checkBox.isChecked());
if (checkBox.isChecked())
savePreferences("CheckBox_Value2", checkBox2.isChecked());
if (checkBox2.isChecked())
savePreferences("CheckBox_Value3", checkBox3.isChecked());
if (checkBox3.isChecked())
savePreferences("CheckBox_Value4", checkBox4.isChecked());
if (checkBox4.isChecked())
savePreferences("CheckBox_Value5", checkBox5.isChecked());
if (checkBox5.isChecked())
savePreferences("CheckBox_Value6", checkBox6.isChecked());
if (checkBox6.isChecked())
savePreferences("CheckBox_Value7", checkBox7.isChecked());
if (checkBox7.isChecked())
savePreferences("storedName", editText.getText().toString());
finish();
}
}
任何帮助都将非常感谢!!!
答案 0 :(得分:1)
保存状态时删除if语句,否则只保存已选中的复选框,我们不希望这样。
// Remove
if (checkBox2.isChecked())
// Without if statement this will save true if checkbox is checked
// false if it is not
savePreferences("CheckBox_Value2", checkBox2.isChecked());
您也可以简化
boolean checkBox7Value = sharedPreferences.getBoolean("CheckBox_Value7", false);
checkBox7.setChecked(checkBox7Value);