SharedPreferences 记住选中状态

来源:互联网 发布:以撒的结合mac下载 编辑:程序博客网 时间:2024/06/07 08:05
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;    }}
原创粉丝点击