如何通过按下完成按钮来记住我的选择?

时间:2014-03-19 20:39:11

标签: android android-layout android-fragments android-listview android-checkbox

我在列表中使用已检查的视图。我有2个切换按钮,点击时更新UI。我想让我的代码让它记住以前选择的值,如果它仍然是相同的完成按钮被禁用,如果它被更改,说我切换到值2,完成按钮启用,反之亦然,它还记得我以前当我稍后返回相同的视图时选择。这是我的代码:

 public class ManageListDashboardFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
        public final static String TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT = "ManageListDashboardFragment";
        private boolean mIsPersonal = true;
        private boolean mIsShared = true;

        private boolean mShouldbeon;
        private boolean mInitialShouldbeon;
        private boolean mShouldbeon1;
        private boolean mInitialShouldbeon1;
        protected Button mPreferencesDoneButton;
        final boolean isPersonal = true;
        final boolean isShared = true;
        private ListsFragment mListsFragment;
        ToggleButton one; 
        ToggleButton two;
        public static ManageListDashboardFragment newInstance(final FragmentManager manager, final int animation) {
            final ManageListDashboardFragment fragment = new ManageListDashboardFragment();
            final Bundle arguments = new Bundle();
            arguments.putInt(ANIMATION, animation);
            fragment.setArguments(arguments);
            final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.manage_news_categories_container);
            fragmentInfo.setFragmentTag(TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT);
            FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
            return fragment;
        }

        @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 int animation = arguments.getInt(ANIMATION, 0);
            final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();
            if (animation != 0) {
                activity.animateTitle(R.id.actionbar_title, arguments.getInt(ANIMATION, 0));
            }
            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(((ManageListDashboardActivity) 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);

            initManageListDashboardFragment();

        }

        private void initManageListDashboardFragment() {
            populateData();
            setupClickListeners();

            Button personalbutton = (Button) getActivity().findViewById(R.id.button_personal_list);
            personalbutton.setOnClickListener(this);
            Button 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);
            two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
            two.setOnCheckedChangeListener(this);


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

        }


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

            switch (view.getId()) {
                case R.id.button_personal_list:
                    mIsPersonal = !mIsPersonal;
                    mIsShared = false;
                    one.setChecked(mIsPersonal);
                    return;

                case R.id.button_shared_list:
                    mIsPersonal = false;
                    mIsShared = !mIsShared;
                    two.setChecked(mIsShared);
                    return;


                case R.id.button_done:
                    saveUserPreferences();

                    DashboardFragment.getInstance().getListsFragment().setIsPersonal(mIsPersonal);

                    break;

                default:
                    break;
            }

            activity.onBackPressed();
        }
        private void saveUserPreferences() {

            final SharedPreferencesManager manager = SharedPreferencesManager.getInstance();
            if (manager.shouldManageListDashboard() == mIsPersonal) {
                manager.shouldManageListDashboard();
            }


        }


        protected void toggleDoneButton() {
            boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
            boolean isDoneEnabled = (
                     mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                    || isUserPreferencesUpdated);
            mPreferencesDoneButton.setEnabled(isDoneEnabled);
        }
        public void onUpdate() {
            mListsFragment.onUpdate();
        }

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

            if(isChecked){
                 if (buttonView == one) {
                        two.setChecked(false);
                    }
                 else if (buttonView == two) {
                        one.setChecked(false);

                    }
                }
            switch (buttonView.getId()) {
                case R.id.personal_list_toggle_control:

                    mShouldbeon = isChecked;
                    if(mShouldbeon = isChecked && mShouldbeon1 != isChecked){


                    }
                    break;
                case R.id.shared_list_toggle_control:

                    mShouldbeon1 = isChecked;
                    if(mShouldbeon1 = isChecked && mShouldbeon != isChecked){
                        }
                    break;

                default:
                    break;
            }
            toggleDoneButton();
        }

    }

1 个答案:

答案 0 :(得分:0)

我建议您在ManageListDashboardFragment中创建2个静态变量,以保存togglebuttons的当前状态。按下完成按钮后更新这些变量。并在initManageListDashboardFragment()中根据静态变量的当前值更新切换按钮的状态。当然,只要您的应用程序正在运行,静态变量值就会被保留。如果您希望甚至在应用程序的不同实例上保留状态,则使用SharedPreference而不是静态变量。

SharedPreferance示例: -

public class ManageListDashboardFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
    public final static String TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT = "ManageListDashboardFragment";
    private boolean mIsPersonal = true;
    private boolean mIsShared = true;
    private SharedPreferences sp;
    private boolean mShouldbeon;
    private boolean mInitialShouldbeon;
    private boolean mShouldbeon1;
    private boolean mInitialShouldbeon1;
    protected Button mPreferencesDoneButton;
    final boolean isPersonal = true;
    final boolean isShared = true;
    private ListsFragment mListsFragment;
    ToggleButton one; 
    ToggleButton two;
    public static ManageListDashboardFragment newInstance(final FragmentManager manager, final int animation) {
        final ManageListDashboardFragment fragment = new ManageListDashboardFragment();
        final Bundle arguments = new Bundle();
        arguments.putInt(ANIMATION, animation);
        fragment.setArguments(arguments);
        final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.manage_news_categories_container);
        fragmentInfo.setFragmentTag(TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT);
        FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
        return fragment;
    }

    @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 int animation = arguments.getInt(ANIMATION, 0);
        final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();
        if (animation != 0) {
            activity.animateTitle(R.id.actionbar_title, arguments.getInt(ANIMATION, 0));
        }
        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(((ManageListDashboardActivity) 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);

        initManageListDashboardFragment();

    }
    Button sharedbutton;
    Button personalbutton;
    private void initManageListDashboardFragment() {
        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);
        boolean toogle = sp.getBoolean("toggle", true);
        two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
        two.setOnCheckedChangeListener(this);
        if(toogle) {    
        one.setChecked(true);
        }
        else {
        two.setChecked(true);
        }

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

    }


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

        switch (view.getId()) {
            case R.id.button_personal_list:
                mIsPersonal = !mIsPersonal;
                mIsShared = false;
                one.setChecked(mIsPersonal);

                sp.edit().putBoolean("toggle", mIsPersonal).commit();
                return;

            case R.id.button_shared_list:
                mIsPersonal = false;
                mIsShared = !mIsShared;
                two.setChecked(mIsShared);
                sp.edit().putBoolean("toggle", !mIsShared).commit();
                return;


            case R.id.button_done:
                saveUserPreferences();

                DashboardFragment.getInstance().getListsFragment().setIsPersonal(mIsPersonal);

                break;

            default:
                break;
        }

        activity.onBackPressed();
    }
    private void saveUserPreferences() {

        final SharedPreferencesManager manager = SharedPreferencesManager.getInstance();
        if (manager.shouldManageListDashboard() == mIsPersonal) {
            manager.shouldManageListDashboard();
        }


    }


    protected void toggleDoneButton() {
        boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
        boolean isDoneEnabled = (
                 mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                || isUserPreferencesUpdated);
        mPreferencesDoneButton.setEnabled(isDoneEnabled);
    }
    public void onUpdate() {
        mListsFragment.onUpdate();
    }

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

        if(isChecked){
             if (buttonView == one) {
                    two.setChecked(false);
                    personalbutton.setEnabled(false);
                    sharedbutton.setEnabled(true);
                    one.setEnabled(false);
                    two.setEnabled(true);
                    sp.edit().putBoolean("toggle", true).commit();
                }
             else if (buttonView == two) {
                    one.setChecked(false);
                    personalbutton.setEnabled(true);
                    sharedbutton.setEnabled(false);
                    two.setEnabled(false);
                    one.setEnabled(true);
                    sp.edit().putBoolean("toggle", false).commit();
                }
            }
        switch (buttonView.getId()) {
            case R.id.personal_list_toggle_control:

                mShouldbeon = isChecked;
                if(mShouldbeon = isChecked && mShouldbeon1 != isChecked){


                }
                break;
            case R.id.shared_list_toggle_control:

                mShouldbeon1 = isChecked;
                if(mShouldbeon1 = isChecked && mShouldbeon != isChecked){
                    }
                break;

            default:
                break;
        }
        toggleDoneButton();
    }

}