如果选择了保存的类别,如何禁用完成按钮?

时间:2014-11-10 15:29:57

标签: android android-layout android-activity android-fragments android-preferences

我正在尝试禁用以前保存/选定类别的完成按钮。 这是我的工作流程:

  
      
  1. 您长按一下视图的标题,弹出一个片段,顶部有完成按钮,另外两个复选框:说个人和共享
  2.   
  3. 现在,如果我选择个人并单击完成按钮,结果将被保存(复选框已保存)。
  4.   
  5. 现在,当我再次长时间点击视图并打开此片段时,应该禁用完成按钮,这样只有在我选择时   共享它将再次变为活动状态,我可以来回切换   当我选择完成按钮时禁用完成按钮。
  6.   
  7. 如果我选择并保存共享选项,在选择共享并激活它时禁用完成按钮,它应该以相同的方式工作   当选择个人时。
  8.   

但是,目前它会保存结果并正确显示所有内容,但不会按预期禁用完成按钮。我正在使用" shouldbeon"," Initialshouldbeon"用于检测此问题的参数,但它没有按预期工作。这是我的代码:

public class ManageListDashboardFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
    public final static String TAG_MANAGE_LIST_CATEGORIES_FRAGMENT = "ManageListDashboardFragment";
    private boolean mIsPersonal = true; 
    private boolean mIsShared = true;
    private boolean isdonebuttonclicked = false;
    public static final String ANIMATION = "animation";
    private boolean mShouldbeon;
    private boolean mInitialShouldbeon;
    private boolean mShouldbeon1;
    private boolean mInitialShouldbeon1;
    protected Button mPreferencesDoneButton;
    private SharedPreferences sp;
    private boolean isPersonal = true;
    private boolean isShared = true;
    Button sharedbutton;
    Button personalbutton;
    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_list_categories_container);
        fragmentInfo.setFragmentTag(TAG_MANAGE_LIST_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() {
        isdonebuttonclicked = false;
        Log.d("KRUSTY3","it is " +isdonebuttonclicked);
        mIsPersonal = isPersonal;
        isdonebuttonclicked = false;
        SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false);
        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);
        SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false);
        final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title);
        titleView.setText(R.string.manage_dashboard_lists);

        initManageListDashboardFragment();

    }

    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 onResume(){
        super.onResume();
    }

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

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

                return;

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


            case R.id.button_done:
                isdonebuttonclicked = true;

                        if(one.isChecked()){
                            DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.PERSONAL);
                            sp.edit().putBoolean("toggle", true).commit();

                        }else{
                            DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.SHARED);
                            sp.edit().putBoolean("toggle", false).commit();

                        }
                }
                break;

            default:

                break;
        }

        activity.onBackPressed();
    }

    //BELOW FUNCTION IS THE MAIN PLACE WHERE DISABLING DONE BUTTON IS DETECTED
    protected void toggleDoneButton() {
        boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
        boolean isDoneEnabled = (
                 mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                || isUserPreferencesUpdated);
        mPreferencesDoneButton.setEnabled(isDoneEnabled);

    }

    public void onUpdate() {
        mListsFragment.onUpdate();
    }

    public Uri onCreateContentUriTest() {
        final Builder builder = ListsContentProvider.LISTS_URI.buildUpon();
        if (mIsPersonal) {
            builder.appendEncodedPath(ListsFragment.PERSONAL);
        } else {
            builder.appendEncodedPath(ListsFragment.SHARED);
        }

        builder.appendQueryParameter(PortfolioList.QueryParams.IS_PERSONAL, String.valueOf(mIsPersonal));
        return builder.build();
    }

    @Override
    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
        mShouldbeon = !mShouldbeon1;
        if(isChecked ){
             if ( buttonView == one ) {
                    two.setChecked(false);
                    one.setEnabled(false);
                    two.setEnabled(true);
                    personalbutton.setEnabled(false);
                    sharedbutton.setEnabled(true);

                }
             else if (buttonView == two) {
                    one.setChecked(false);
                    two.setEnabled(false);
                    one.setEnabled(true);
                    personalbutton.setEnabled(true);
                    sharedbutton.setEnabled(false);

                }
            }
        switch (buttonView.getId()) {

            case R.id.personal_list_toggle_control:

                mShouldbeon = isChecked;

                break;
            case R.id.shared_list_toggle_control:

                mShouldbeon1 = isChecked;

                break;

            default:
                break;
        }
        toggleDoneButton();
    }

}

3 个答案:

答案 0 :(得分:0)

根据我的理解,您有两个复选框,只有在至少一个复选框的状态发生变化时才需要启用“完成”按钮。因此,如果您将两个复选框的原始状态存储为 personalChecked sharedChecked ,那么在更新任意两个复选框时,您可以运行类似这样的

mPreferencesDoneButton.setEnabled((personalChecked ^ one.isChecked()) || (sharedChecked ^ two.isChecked()))

答案 1 :(得分:0)

据我了解你的问题,我认为这可能会这样做:

每当你在longclick上打开对话框时,检查是否检查了任何ToggleButton,如果是,则设置:

doneButton.setClickable(false);

如果用户选择其他ToggleButton,请再次启用它!

答案 2 :(得分:0)

使用setvisiblity()。

  public void onClick(View v) {
         //when saved category is clicked hide done button
         doneButton.setVisibility(View.GONE);
    }

您可以再次启用按钮

         doneButton.setVisibility(View.VISIBLE);