无法在复选框选择中设置共享首选项中的值

时间:2014-12-08 05:33:22

标签: android

public class FragSettings extends SherlockFragment {
    CheckBox notificationcheckbox;
    Button savebutton;
    SharedPreferences preferences;
    Editor editor;

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

        preferences = getActivity().getSharedPreferences(
                AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);

        View view = inflater.inflate(R.layout.frag_settings, container, false);
        notificationcheckbox = (CheckBox) view.findViewById(R.id.checkBox1);
        savebutton = (Button) container.findViewById(R.id.button1);
        editor = preferences.edit();
        editor.putString("NotifcationValue", "1");

        addListenerOnChkWindows();

        return view;
    }

    public void addListenerOnChkWindows() {

        notificationcheckbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (notificationcheckbox.isChecked()) {

                    Editor editor = preferences.edit();
                    editor.putString("NotifcationValue", "1");

                    editor.commit();

                    Toast.makeText(getActivity(), "On", Toast.LENGTH_LONG)
                            .show();
                }

                else {
                    editor.putString("NotifcationValue", "0");
                    Toast.makeText(getActivity(), "Off", Toast.LENGTH_LONG)
                            .show();

                }
            }
        });

    }

}

这里是我的代码我想在Cehcbox上设置值启用值应该在共享首选项中设置1并且复选框禁用值应该设置0我已经尝试但我能够设置值但是在复选框上禁用值不设置0请查看我的代码并告诉我哪里有问题请告诉我代码。

3 个答案:

答案 0 :(得分:2)

添加字符串后,您应该提交preferences

 editor.commit(); 

答案 1 :(得分:1)

你错过了

 editor.commit();
else正文

中的

onCheckedChangeListener

也使用onClickListener代替CheckBox 像这样

notificationcheckbox
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        Editor editor = preferences.edit();
                        editor.putString("NotifcationValue", "1");
                        editor.commit();
                        Toast.makeText(getActivity(), "On",
                                Toast.LENGTH_LONG).show();
                    } else {
                        editor.putString("NotifcationValue", "0");
                        editor.commit();
                        Toast.makeText(getActivity(), "Off",
                                Toast.LENGTH_LONG).show();
                    }

                }
            });

答案 2 :(得分:0)

 please use onCkeck change listener with check box like

    public class FragSettings extends SherlockFragment {
        CheckBox notificationcheckbox;
        Button savebutton;


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



            View view = inflater.inflate(R.layout.frag_settings, container, false);
            notificationcheckbox = (CheckBox) view.findViewById(R.id.checkBox1);
            savebutton = (Button) container.findViewById(R.id.button1);


            addListenerOnChkWindows();

            return view;
        }

        public void addListenerOnChkWindows() {


    notificationcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


    if(isChecked)
    {
    SharedPreferences prefs = context.getSharedPreferences("Notificationpref", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("NotifcationValue", "1");
            editor.commit();
    Toast.makeText(getActivity(), "On", Toast.LENGTH_LONG)
                                .show();
    }
    else
    {
      SharedPreferences prefs = context.getSharedPreferences("Notificationpref", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("NotifcationValue", "0");
            editor.commit();
                        Toast.makeText(getActivity(), "Off", Toast.LENGTH_LONG)
                                .show();
    }

                }
            });




        }

    }