samedi 25 juillet 2015

How to save status of checkbox in android

I have gone through many questions on StackOverflow but could not get this code working. The checkbox is always checked when the activity is created.

My Code :

    @Override
    public void onStart() {
        super.onStart();
        chb1.setChecked(load1());
    }

    @Override
    public void onPause() {
        super.onPause();
        save1(chb1.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
       chb1.setChecked(load1());
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_lock);
chb1 = (CheckBox)  findViewById(R.id.checkBox2);
chb1.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(chb1.isChecked())
                {
                    SharedPreferences sharedPreferences1 = getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences1.edit();
                    editor.putBoolean("checked", true);
                    editor.commit();
}

How can i make this work? Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire