How to save a checkbox state in android app

前端 未结 3 378
醉话见心
醉话见心 2020-12-17 07:13

I have this app with a simple checkbox which adds a line to build.prop when the user presses it , the thing works but when i exit the app the check box is reseted , is there

3条回答
  •  萌比男神i
    2020-12-17 07:49

    Yes, you can Just save the value of checkBox state in SharedPreferences and then retrieve them later when required

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

    later you can get it like

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

    .See How to use SharedPreferences in Android to store, fetch and edit values


    You can use putBoolean() and getBoolean() methods of Sharedpreferences too as checkboxes have only two states

提交回复
热议问题