简体   繁体   中英

How to save the state of the checkbox when it is checked and a button pressed

Aim is to save the background color of a textview whenever a checkbox is checked and a button is pressed while it will revert back to its normal state when redoing it.

I know that to do that i can use shared preferences but somehow it doesn't work (NOT SAVED). Here is the codes that i have used (checkbox is created programtically not though xml)

status=(Button)findViewById(R.id.status);
CheckBox checkbox = new CheckBox(myContext);
tr.addView(checkbox);

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked){
            status.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //myEditor.putInt("backColor", Color.LTGRAY);
                    //tr.setBackgroundColor(Color.LTGRAY);
                    mySharedPreferences=getSharedPreferences(MYPREFS,0);
                    SharedPreferences.Editor myEditor;
                    myEditor=mySharedPreferences.edit();
                    final int backColor=mySharedPreferences.getInt("color", Color.LTGRAY);
                    tr.setBackgroundColor(backColor);
                    myEditor.putInt("color", backColor);
                    myEditor.commit();
                }
            });
        }
    }

}

im not sure of what you are trying to say, but you could try this.

boolean check = :JCheckBox reference:.isSelected();

if JCheckBox reference was checkBox, then it would look like this.

boolean check = checkBox.isSelected();

method returns a boolean value.

How i would do it:

  • You have your checkbox set up (i will call it checkBox ) And
  • you have your color editor (it will be colorEditor )

So first you need to crate two entries in the SharedPreferences: DEFAULT_COLOR and CURRENT_COLOR

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("DEFAULT_COLOR", 0xFFFFFFFF); // we will not touch this later
editor.putInt("CURRENT_COLOR", 0xFFFFFFFF); //set to DEFAULT, will change this.
editor.commit();

Now now in the onCheckedChanged you simply need to do this:

public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
{
    SharedPreferences preferences = ...; //just get the SharedPref. object
    SharedPreferences.Editor editor = preferences.edit();
    int color = preferences.getInt("DEFAULT_COLOR", -1); //get the default color

    // change it if the CheckBox is checked
    if(isChecked)
        color = colorEditor.getColor(); //get the color from wherever you want

    editor.putInt("CURRENT_COLOR", color);
    editor.commit();

    //set the color as background.
}

Also don't forget to set the background color to the "CURRENT_COLOR" from your SharedPreferences every time you start the app!

Also it's a good practice to add every View programmatically OR from XML, becuse it's a big soure of errors!

package com.android.app;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.app.Activity;
import android.content.SharedPreferences;

public class MainActivity extends Activity {
CheckBox cb;
Button save,load;
SharedPreferences sp;
public static String filename=("Folder");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cb=(CheckBox)findViewById(R.id.checkBox1);
    save=(Button)findViewById(R.id.bsave);
    load=(Button)findViewById(R.id.bload);
    sp=getSharedPreferences(filename,0);
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            boolean b=cb.isChecked();
            SharedPreferences.Editor editor=sp.edit();
            editor.putBoolean("str",b);
            editor.commit();
            finish();

        }
    });
    load.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sp=getSharedPreferences(filename,0);
            boolean bool=sp.getBoolean("str",false);
            cb.setChecked(bool);

        }
    });
}

}

I have tested this; it works.

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