简体   繁体   中英

The SharedPreferences doesn't save the checked box in my Android app

I have a check box in an AlertDialog that starts when the app starts. I added "SharedPreferences" to save if the check box is checked or not. If the check box checked, the AlertDialog never starts or shows when the app starts. But my problem is: When I check that box, the AlertDialog starts in every time when I launch the app.

checkbox.xml (layout file):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/checkboxDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        style="?android:attr/textAppearanceMedium" />

</LinearLayout>

The onCreate method in the MainActivity.java :

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

        View checkBoxView = View.inflate(this, R.layout.checkbox, null);
        final CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkboxDialog);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences prefs = getSharedPreferences("state", 0);

                if(prefs.getBoolean("x", false) == true)
                {
                    checkBox.setChecked(true);
                }
                else if(checkBox.isChecked() == true)
                {
                    SharedPreferences myPrefs = getSharedPreferences("state", 0);
                    SharedPreferences.Editor editor = myPrefs.edit();
                    editor.putBoolean("x", true);
                    editor.commit();
                }
            }
        });
        checkBox.setText("Test");
        final Context context = this;
        AlertDialog.Builder mainDialog = new AlertDialog.Builder(context);
        mainDialog.setTitle("Test");
        mainDialog.setMessage("Test my dialog!");
        mainDialog.setView(checkBoxView);
        mainDialog.setPositiveButton("Go to website", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
                startActivity(browseIntent);
            }
        });
        AlertDialog alertDialog = mainDialog.create();
        alertDialog.show();

        ...
    }

I found the answer here:

Hope it will help others.

Thanks.

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