SharedPreferences 记住选中状态

public class FragmentThree extends Fragment{

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private CheckBox cb4;
    private Button bt_sw;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View view = View.inflate(getActivity(), R.layout.frag_three,null);

        cb1 = (CheckBox) view.findViewById(R.id.cb1);
        cb2 = (CheckBox) view.findViewById(R.id.cb2);
        cb3 = (CheckBox) view.findViewById(R.id.cb3);
        cb4 = (CheckBox) view.findViewById(R.id.cb4);
        bt_sw = (Button) view.findViewById(R.id.bt_sw);

        preferences = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = preferences.edit();

        Boolean check1 = preferences.getBoolean("flag1",false);
        if(check1){
            cb1.setChecked(true);
        }

        Boolean check2 = preferences.getBoolean("flag2",false);
        if(check2){
            cb2.setChecked(true);
        }
        Boolean check3 = preferences.getBoolean("flag3",false);
        if(check3){
            cb3.setChecked(true);
        }
        Boolean check4 = preferences.getBoolean("flag4",false);
        if(check4){
            cb4.setChecked(true);
        }

        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b){
                    editor.putBoolean("flag1",true);
                    editor.commit();
                }else{
                    editor.putBoolean("flag1",false);
                    editor.commit();
                }
            }
        });

        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b){
                    editor.putBoolean("flag2",true);
                    editor.commit();
                }else{
                    editor.putBoolean("flag2",false);
                    editor.commit();
                }
            }
        });

        cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b){
                    editor.putBoolean("flag3",true);
                    editor.commit();
                }else{
                    editor.putBoolean("flag3",false);
                    editor.commit();
                }
            }
        });

        cb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b){
                    editor.putBoolean("flag4",true);
                    editor.commit();
                }else{
                    editor.putBoolean("flag4",false);
                    editor.commit();
                }
            }
        });

        bt_sw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(getActivity(), HttpActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值