我已经实现了一个带有多个布局的多个复选框的应用程序。当用户取消选中相关的复选框时,我看不到布局。然后我尝试在用户重新启动应用程序时保存内容和设置。
我已经实现了如下代码:
public class MultipleControllsActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout l1,l2,l3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1 = ((LinearLayout)findViewById(R.id.linearLayout1));
l2 = ((LinearLayout)findViewById(R.id.linearLayout2));
l3 = ((LinearLayout)findViewById(R.id.linearLayout3));
((CheckBox)findViewById(R.id.checkBox1)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isChecked){
l1.setVisibility(View.GONE);
}
}
});
((CheckBox)findViewById(R.id.checkBox2)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l2.setVisibility(View.GONE);
}
});
((CheckBox)findViewById(R.id.checkBox3)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l3.setVisibility(View.GONE);
}
});
((Button)findViewById(R.id.saveTemp)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Save the content code here
}
});
}
}
当我点击saveTemp按钮然后我想保存内容,如果用户重新启动应用程序,则会显示保存的内容。
请任何人帮助我......
答案 0 :(得分:3)
您可以使用
SharedPreferences
http://developer.android.com/reference/android/content/SharedPreferences.html
使用此: -
public class MultipleControllsActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout l1,l2,l3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences pref_settings;
pref_settings = getSharedPreferences("DATA", Context.MODE_PRIVATE);
l1 = ((LinearLayout)findViewById(R.id.linearLayout1));
l2 = ((LinearLayout)findViewById(R.id.linearLayout2));
l3 = ((LinearLayout)findViewById(R.id.linearLayout3));
l1.setVisibility(pref_settings.getInt("l1", 0));
l2.setVisibility(pref_settings.getInt("l2", 0));
l2.setVisibility(pref_settings.getInt("l3", 0));
((CheckBox)findViewById(R.id.checkBox1)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isChecked){
l1.setVisibility(View.GONE);
}
}
});
((CheckBox)findViewById(R.id.checkBox2)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l2.setVisibility(View.GONE);
}
});
((CheckBox)findViewById(R.id.checkBox3)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l3.setVisibility(View.GONE);
}
});
((Button)findViewById(R.id.saveTemp)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref_settings;
pref_settings = getSharedPreferences("DATA", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref_settings.edit();
editor.putInt("l1", l1.getVisibility());
editor.putInt("l2", l2.getVisibility());
editor.putInt("l3", l3.getVisibility());
editor.commit();
}
});
}
答案 1 :(得分:0)
以下是我使用Multiple Choice ListView实现的示例,即使我不直接使用CheckBoxes,您可能会对如何继续进行一些了解。 您可以像这样保存您的偏好......
public void SaveSelections() {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sp.edit();
String savedItems = getSavedItems();
prefEditor.putString(LOAD_LIST, savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = listView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (listView.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + listView.getItemAtPosition(i);
} else {
savedItems += listView.getItemAtPosition(i);
}
}
}
return savedItems;
}
以下是加载选择的方法..
private void LoadSelections() {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
if (sp.contains(LOAD_LIST)) {
String savedItems = sp.getString(LOAD_LIST, "");
this.selectedItems.addAll(Arrays.asList(savedItems.split(",")));
int count = this.listView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) listView.getAdapter().getItem(i);
if (this.selectedItems.contains(currentItem)) {
this.listView.setItemChecked(i, true);
}
}
}
}
然后在OnCreate中调用LoadSelections(),这样......
Bundle b = new Bundle();
checkedItems = listView.getCheckedItemPositions();
final int checkedItemsCount = checkedItems.size();
lst = new ArrayList<String>();
for (int i = 0; i < checkedItemsCount; ++i) {
// This tells us the item position we are looking at
final int position = checkedItems.keyAt(i);
final String str2 = (String) listView.getItemAtPosition(position);
lst.add(str2);
}
SaveSelections();
最后在OnCreate中调用LoadSelections,就像这样
LoadSelections();