简体   繁体   中英

how to make checkbox stay checked when program closes?

在我的程序中,我有大约20个复选框,但是当我选中它们并关闭程序然后再返回到复选框时,它们始终处于未选中状态,如何使该复选框保持选中状态。

In your onPause function, write the states to a file or to a SharedPreference. In your onResume, read that file/Preference in and set the checkboxes. You may want to look at PreferenceActivity to do this for you.

You can use SharedPreferences to do this.

So first, when your application goes to onPause , you'll want to store the state of each checkbox in SharedPreferences :

SharedPreferences prefs = this.getSharedPreferences("com.example.myapp", Context.MODE_PRIVATE);
prefs.putBoolean("checkbox1", checkbox1.isChecked()).commit();
// do this for all 20

Then you can check the checkboxes onResume and onCreate :

checkbox1.setChecked(prefs.getBoolean("checkbox1", false));

You should look into using Shared Preferences .

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Writing to Shared Preferences :

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

Reading from Shared Preferences

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

Also here is a very simple tutorial on implementing them.

Requested Example Code :

Main Activity Class :

package com.zeus.example;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.CheckBox;

public class MainActivity extends Activity {

CheckBox chkBox1, chkBox2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    chkBox2 = (CheckBox) findViewById(R.id.checkBox2);

    SharedPreferences getPrefs = 
            PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    boolean value = getPrefs.getBoolean("checkbox_preference", true);
    chkBox1.setSelected(value); // Setting the value reflected from the preference
    chkBox2.setSelected(!value); // Setting the value opposite of the preference(Just an example)
}
}

Preference Activity Class :

package com.zeus.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity{

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       addPreferencesFromResource(R.xml.settings);
   }
}

Settings XML :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference
               android:key="checkbox_preference"
               android:defaultValue="false"
              android:title="CheckBox1"
        />
</PreferenceScreen>

Although this method is easy, it's deprecated and is recommended to use Preference Fragments instead

Use SharedPreferences . The simplest way to store small values forever.

Since you have multiple check boxes use SharedPreferences.Editor putStringSet (String key, Set<String> values)

You can use SimpleSharedPreferences in which putStringSet is backported to API-1.

Usage: No Edit, No Commit.

SimpleSharedPreferences mPreferences = new SimpleSharedPreferences(getApplicationContext()); //Init.

mPreferences.putStringSet("STRING_SET_KEY", mStringSet); //Single Line 

mPreferences.getStringSet("STRING_SET_KEY", null); // Get String set.

mPreferences.putBoolean("INTEGER_BOOL", true); //Just an example.

Note : I am the author of SimpleSharePreferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM