将复选框状态保存到 Android 中的 SharedPreferences 文件 [英] Save CheckBox State to SharedPreferences File in Android

查看:52
本文介绍了将复选框状态保存到 Android 中的 SharedPreferences 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保存几个复选框,以便当用户再次打开应用程序时,他们可以看到他们离开应用程序的状态.我尝试使用首选项,但似乎无法得到正确的结果.

I have a couple of check boxes that I need to save so that when the user opens the Application again, they can see the state that they left the application in. I have tried using the preferences but I can't seem to get the result correctly.

MainActivity.java

MainActivity.java

package com.example.android.documentchecklist;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

    CheckBox allotment, sscMarkList, hscMarkList, leaving, profomaCap, jeeScoreCard, gapCer, casteCer, casteVal, nonCreamyL, domicileCertificate, photograph, migration, adhaarCard, nationalityCertificate;
    boolean hasAllotment, hasSscMarkList, hasHscMarkList, hasLeaving, hasProfoma, hasJeeScore, hasGapCertificate, hasCasteCertificate, hasCasteValidity, hasNonCreamyLayer, hasDomicileCertificate, hasPhoto, hasMigCertificate, hasadhaarCard, hasNationalityCertificate;

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

        allotment = (CheckBox) findViewById(R.id.allot);
        sscMarkList = (CheckBox) findViewById(R.id.ssc);
        hscMarkList = (CheckBox) findViewById(R.id.hsc);
        leaving = (CheckBox) findViewById(R.id.leaving);
        profomaCap = (CheckBox) findViewById(R.id.profoma);
        jeeScoreCard = (CheckBox) findViewById(R.id.jeeScore);
        gapCer = (CheckBox) findViewById(R.id.gapCertificate);
        casteCer = (CheckBox) findViewById(R.id.casteCertificate);
        casteVal = (CheckBox) findViewById(R.id.casteValidity);
        nonCreamyL = (CheckBox) findViewById(R.id.nonCreamyLayer);
        adhaarCard = (CheckBox) findViewById(R.id.adhaarCard);
        nationalityCertificate = (CheckBox) findViewById(R.id.nationalityCer);
        domicileCertificate = (CheckBox) findViewById(R.id.domicile);
        photograph = (CheckBox) findViewById(R.id.photo);
        migration = (CheckBox) findViewById(R.id.migration);

    }


    public void checkBoxClicked(View view) {

        int id = view.getId();

        if (id == R.id.mahaState) {

            migration.setVisibility(View.GONE);

            allotment.setText(getString(R.string.allot));
            allotment.setVisibility(View.VISIBLE);
            hasAllotment = allotment.isChecked();


            sscMarkList.setText(getString(R.string.ssc));
            sscMarkList.setVisibility(View.VISIBLE);
            hasSscMarkList = sscMarkList.isChecked();

            hscMarkList.setText(getString(R.string.hsc));
            hscMarkList.setVisibility(View.VISIBLE);
            hasHscMarkList = hscMarkList.isChecked();

            leaving.setText(getString(R.string.leaving));
            leaving.setVisibility(View.VISIBLE);
            hasLeaving = leaving.isChecked();

            profomaCap.setText(getString(R.string.proforma));
            profomaCap.setVisibility(View.VISIBLE);
            hasProfoma = profomaCap.isChecked();

            jeeScoreCard.setText(getString(R.string.jee));
            jeeScoreCard.setVisibility(View.VISIBLE);
            hasJeeScore = jeeScoreCard.isChecked();

            gapCer.setText(getString(R.string.gap_cert));
            gapCer.setVisibility(View.VISIBLE);
            hasGapCertificate = gapCer.isChecked();

            casteCer.setText(getString(R.string.caste_cert));
            casteCer.setVisibility(View.VISIBLE);
            hasCasteCertificate = casteCer.isChecked();

            casteVal.setText(getString(R.string.caste_validity));
            casteVal.setVisibility(View.VISIBLE);
            hasCasteValidity = casteVal.isChecked();

            nonCreamyL.setText(getString(R.string.non_creamy));
            nonCreamyL.setVisibility(View.VISIBLE);
            hasNonCreamyLayer = nonCreamyL.isChecked();

            adhaarCard.setText(getString(R.string.aadhar));
            adhaarCard.setVisibility(View.VISIBLE);
            hasadhaarCard = adhaarCard.isChecked();

            nationalityCertificate.setText(getString(R.string.nationality_cert));
            nationalityCertificate.setVisibility(View.VISIBLE);
            hasNationalityCertificate = nationalityCertificate.isChecked();

            domicileCertificate.setText(getString(R.string.domicile));
            domicileCertificate.setVisibility(View.VISIBLE);
            hasDomicileCertificate = domicileCertificate.isChecked();

            photograph.setText(getString(R.string.photos));
            photograph.setVisibility(View.VISIBLE);
            hasPhoto = photograph.isChecked();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        save(allotment.isChecked());
        save(sscMarkList.isChecked());
        save(hscMarkList.isChecked());
        save(leaving.isChecked());
        save(profomaCap.isChecked());
        save(jeeScoreCard.isChecked());
        save(gapCer.isChecked());
        save(casteCer.isChecked());
        save(casteVal.isChecked());
        save(nonCreamyL.isChecked());
        save(domicileCertificate.isChecked());
        save(photograph.isChecked());
        save(migration.isChecked());
        save(adhaarCard.isChecked());
        save(nationalityCertificate.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        allotment.setChecked(load());
        sscMarkList.setChecked(load());
        sscMarkList.setChecked(load());
        hscMarkList.setChecked(load());
        leaving.setChecked(load());
        profomaCap.setChecked(load());
        jeeScoreCard.setChecked(load());
        gapCer.setChecked(load());
        casteCer.setChecked(load());
        casteVal.setChecked(load());
        nonCreamyL.setChecked(load());
        domicileCertificate.setChecked(load());
        photograph.setChecked(load());
        migration.setChecked(load());
        adhaarCard.setChecked(load());
        nationalityCertificate.setChecked(load());


    }

    private void save(final boolean isChecked) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("check", isChecked);
        editor.apply();
    }

    private boolean load() {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("check", false);
    }

}

感谢您的帮助.

推荐答案

您正在使用单个 keycheck 来存储您的所有 checkbox> state 所以只有这个调用的状态 save(nationalityCertificate.isChecked()); 会被保存,所以你需要为不同的 checkboxes 使用不同的 keys/代码>

you are using the single key i.e. check to store your all checkbox state so only the state of this call save(nationalityCertificate.isChecked()); will be saved ,so you need to use different keys for different checkboxes

例如

    // use different keys to store state of different check boxes
    save(allotment.isChecked(),"allotment");
    save(sscMarkList.isChecked(),"sscMarkList");

    // use same keys to fetch values which were used during save function call
    allotment.setChecked(load("allotment"));
    sscMarkList.setChecked(load("sscMarkList"));


private void save(final boolean isChecked, String key) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, isChecked);
    editor.apply();
}

private boolean load(String key) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean(key, false);
}

注意:您还可以在 onCreateonStart 中初始化您的共享首选项,例如您的 CheckBox 视图,而不是每次都重新初始化它保存时间

Note: you can also initialize your shared preference like your CheckBox views in onCreate or onStart only once instead of re-initializing it every time in save

这篇关于将复选框状态保存到 Android 中的 SharedPreferences 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆