如何保存所选复选标记的状态?

时间:2014-10-16 16:42:05

标签: android android-fragments android-fragmentactivity android-preferences android-checkbox

我有我的代码,其中有2个复选框。当选择其中一个时,另一个未被选中,反之亦然。它运行正常,除了当我点击这个片段上的后退按钮时,它以某种方式保存了最后一个选中复选框的状态(不按预期更新内容,只有当我点击完成按钮时才应该这样做)。此外,当我引用我的页面或更改我的平板电脑方向时,它将恢复为默认页面(在这种情况下为个人列表)。我想让它工作,不仅保存所选复选框的状态,而且还保存相关的片段内容。此外,点击后,它不应该保存任何东西,除非我点击完成按钮。我知道要在sharepreferencesmanager中完成,但我不知道如何去做以及在哪里拨打电话,有没有人有线索?这是我的相同代码:

public class Listfragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
        public final static String TAG_MANAGE_LIST_CATEGORIES_FRAGMENT = "Listfragment";
        private boolean mIsPersonal = true; 
        private boolean mIsShared = true;
        public static final String ANIMATION = "animation";
        private boolean mShouldbeon;
        private boolean mInitialShouldbeon;
        private boolean mShouldbeon1;
        private boolean mInitialShouldbeon1;
        protected Button mPreferencesDoneButton;
        private SharedPreferences sp;
        final boolean isPersonal = true;
        final boolean isShared = true;
        Button sharedbutton;
        Button personalbutton;
        private ListsFragment mListsFragment;
        ToggleButton one; 
        ToggleButton two;


        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.fragment_manage_lists, container, false);
            final Bundle arguments = getArguments();
            final ListActivity activity = (ListActivity) getActivity();


            return view;
        }
        protected void setupClickListeners() {
            mIsPersonal = isPersonal;
            mPreferencesDoneButton = (Button) getActivity().findViewById(R.id.button_done);
            Typeface face = Typeface.createFromAsset(mPreferencesDoneButton.getContext().getAssets(),
                    "fonts/proxima-nova-regular.ttf");
            mPreferencesDoneButton.setTypeface(face);
            mPreferencesDoneButton.setOnClickListener(this);
            mPreferencesDoneButton.setEnabled(((ListActivity) getActivity()).isDoneButtonEnabled());
        }
        @Override
        public void onActivityCreated(final Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title);
            titleView.setText(R.string.manage_dashboard_lists);

            initListfragment();

        }

        private void initListfragment() {
            populateData();
            setupClickListeners();
            sp = PreferenceManager.getDefaultSharedPreferences(getActivity());

             personalbutton = (Button) getActivity().findViewById(R.id.button_personal_list);
            personalbutton.setOnClickListener(this);
             sharedbutton = (Button) getActivity().findViewById(R.id.button_shared_list);
            sharedbutton.setOnClickListener(this);
            one = (ToggleButton) getView().findViewById(R.id.personal_list_toggle_control);
            one.setOnCheckedChangeListener(this);
            Log.d("LISTY", "1");
            boolean toogle = sp.getBoolean("toggle", true);
            two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
            two.setOnCheckedChangeListener(this);
            Log.d("LISTY", "2");

            if(toogle){
                one.setChecked(true);
                Log.d("LISTY", "3");
            }
                else{
                two.setChecked(true);
                Log.d("LISTY", "4");
                }


        }
        protected void populateData() {
            SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false);

        }

        @Override
        public void onClick(final View view) {
            final ListActivity activity = (ListActivity) getActivity();

            switch (view.getId()) {
                case R.id.button_personal_list:
                    mIsPersonal = true;
                    mIsShared = false;
                    one.setChecked(true);
                    Log.d("LISTY", "5");

                    sp.edit().putBoolean("toggle", mIsPersonal).commit();
                   Log.d("listdash", "Personal List is selected in onclick : "+mIsPersonal);

                    return;

                case R.id.button_shared_list:
                    mIsPersonal = false;
                    mIsShared = true;
                    two.setChecked(true);
                    sp.edit().putBoolean("toggle", !mIsShared).commit();
                    Log.d("listdash", "Shared List is selected in onclick : "+mIsShared);
                    Log.d("LISTY", "6");
                    return;


                case R.id.button_done:
                    Log.d("LISTY", "7");
                    boolean tabletSize = Utils.isTablet(activity);
                    int layoutType = Application.getAppResources().getInteger(R.integer.layout_type);

                            if(one.isChecked()){
                                DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.PERSONAL);
                                Log.d("LISTY", "8");
                            }else{
                                DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.SHARED);
                                Log.d("LISTY", "9");

                    }
                    break;

                default:

                    break;
            }

            activity.onBackPressed();
            Log.d("LISTY", "10");
        }


        protected void toggleDoneButton() {
            boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
            Log.d("LISTY", "13");
            boolean isDoneEnabled = (
                     mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                    || isUserPreferencesUpdated);
            mPreferencesDoneButton.setEnabled(isDoneEnabled);
            Log.d("LISTY", "14");

        }

        public void onUpdate() {
            mListsFragment.onUpdate();
            Log.d("LISTY", "15");
        }


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

            if(isChecked ){
                 if ( buttonView == one ) {
                        two.setChecked(false);
                        one.setEnabled(false);
                        two.setEnabled(true);
                        Log.d("LISTY", "18");
                        personalbutton.setEnabled(false);
                        sharedbutton.setEnabled(true);
                        sp.edit().putBoolean("toggle", true).commit();
                        Log.d("listdash", "Personal List is selected in onchecked : "+mIsPersonal +one +two);

                    }
                 else if (buttonView == two) {
                        one.setChecked(false);
                        two.setEnabled(false);
                        one.setEnabled(true);
                        Log.d("LISTY", "19");
                        personalbutton.setEnabled(true);
                        sharedbutton.setEnabled(false);
                        sp.edit().putBoolean("toggle", false).commit();
                        Log.d("listdash", "Shared List is selected in oncheck : "+mIsShared +one +two);

                    }
                }
            switch (buttonView.getId()) {

                case R.id.personal_list_toggle_control:

                    mShouldbeon = isChecked;
                    Log.d("LISTY", "20");

                    break;
                case R.id.shared_list_toggle_control:

                    mShouldbeon1 = isChecked;
                    Log.d("LISTY", "21");

                    break;

                default:
                    break;
            }
            toggleDoneButton();
        }

谢谢! P.S:如果你可以解释一下我的代码,那将会很棒。

1 个答案:

答案 0 :(得分:-1)

您可以将代码放在onResume中,这样您的应用就可以根据需要使用

@Override
public void onResume(){
    super.onResume();
    // put your code here...

}