如何在Android应用程序中保存复选框状态

我有这个带有一个简单复选框的应用程序,当用户按下它时,它会为build.prop添加一行,但是当我退出应用程序时,复选框被重置,有没有办法通过添加示例代码来防止这种情况发生你的post,因为我仍然是Android应用程序的新手。

MainActivity.java

package com.mythi.tests; import java.io.DataOutputStream; import java.io.IOException; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onCheckboxClicked(View view) { // Is the view now checked? boolean checked = ((CheckBox) view).isChecked(); // Check which checkbox was clicked switch(view.getId()) { case R.id.checkBox1: if (checked){ try{ Process su = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes("cp /system/build.prop /system/build.prop.bak\n"); outputStream.writeBytes("echo 'persist.sys.scrollingcache=3' >> /system/build.prop\n"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); su.waitFor(); }catch(IOException e){ }catch(InterruptedException e){ } }else{ try{ Process su = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes("rm -r /system/build.prop\n"); outputStream.writeBytes("mv /system/build.prop.bak /system/build.prop\n"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); su.waitFor(); }catch(IOException e){ }catch(InterruptedException e){ } break; } } } } 

activity_main.xml中

    

你在哪里使用getRuntime()。exec()得到了这种非常奇怪的方法? 这是一个使用SharedPreferences的简单工作代码示例。 用这个替换整个switch语句:

 switch(view.getId()) { case R.id.checkBox1: PreferenceManager.getDefaultSharedPreferences(this).edit() .putBoolean("checkBox1", checked).commit(); break; } 

在onCreate()里面添加:

 CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1); boolean checked = PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("checkBox1", false); checkBox1.setChecked(checked); 

完成。

您可以将值保存在首选项中,并在加载时绑定该值。 例如,

 boolean checkedFlag = Preference.getBoolean("checkboxstate",false); CheckBox cb = (CheckBox) findViewById(R.id.checkBox1); cb.setChecked(checkedFlag); 

用于优先设置值

 SharedPreferences Preference = getSharedPreferences("pref", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = Preference.edit(); editor.putBoolean("checkboxstate", checkboxstate); editor.commit(); 

追溯价值

 SharedPreferences Preference = getSharedPreferences("pref", Activity.MODE_PRIVATE); boolean isCheckboxSet = Preference.getBoolean("checkboxstate"); 

是的,您可以在SharedPreferences中保存checkBox状态的值,然后在需要时稍后检索它们

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("state", "checked"); editor.commit(); 

以后你可以得到它

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); String str = sp.getString("state"); 

。 查看如何在Android中使用SharedPreferences来存储,获取和编辑值


您也可以使用putBoolean()getBoolean()方法,因为checkboxes只有two states