简体   繁体   English

如何使切换按钮像单选按钮一样工作?

[英]How do I make my toggle button act like a radio button?

I have 2 toggle buttons in my code and I want to change the behavior such that when one is selected the other one is not and vice versa. 我的代码中有2个切换按钮,并且我想更改行为,以便在选择一个时不选择另一个,反之亦然。 (want it to work like a radio button ).Any idea on how I can go about it? (希望它像单选按钮一样工作)。关于我该如何处理的任何想法? Here's my java code for the same: 这是我的相同的Java代码:

public class DashboardManageListFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {

    public final static String TAG_MANAGE_DASHBOARD_LIST_FRAGMENT = "ManageDashboardListFragment";
    public final static String IS_PERSONAL = "IsPersonal";
    public final static String IS_SHARED = "IsShared";
    public static final String ANIMATION = "animation";
    private boolean mShouldbeon;
    private boolean mInitialShouldbeon;
    protected Button mPreferencesDoneButton;




    public static DashboardManageListFragment newInstance(final FragmentManager manager, final boolean isPersonal) {
        final DashboardManageListFragment fragment = new DashboardManageListFragment();
        final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.fragment_container);
        fragmentInfo.setFragmentTag(TAG_MANAGE_DASHBOARD_LIST_FRAGMENT);
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_PERSONAL, isPersonal);
        fragment.setArguments(bundle);
        fragmentInfo.setAnimation(R.anim.no_animation, R.anim.no_animation);
        fragmentInfo.setPopAnimation(R.anim.no_animation, R.anim.no_animation);
        FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);

        return fragment;
    }



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

        getArguments().getBoolean(IS_PERSONAL, true);

        final Bundle arguments = getArguments();
        final int animation = arguments.getInt(ANIMATION, 0);
        final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();
        /*if(!personalToggle.isChecked()){
        view.findViewById(R.id.personal_list_toggle_control).setVisibility(View.GONE);
        }*/
        return view;
    }
    protected void setupClickListeners() {
        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 onStart() {
        super.onStart();
        setupClickListeners();
        final ToggleButton personalToggle = (ToggleButton) getView().findViewById(R.id.personal_list_toggle_control);
        personalToggle.setOnCheckedChangeListener(this);

        final ToggleButton sharedToggle = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
        sharedToggle.setOnCheckedChangeListener(this);

        if(personalToggle.isChecked() == true){
        }else{
            //getActivity().findViewById(R.id.personal_list_toggle_control).setVisibility(View.GONE);
            sharedToggle.isChecked();
        }
        if(sharedToggle.isChecked()){

            }else{
                //getActivity().findViewById(R.id.shared_list_toggle_control).setVisibility(View.GONE);
                personalToggle.isChecked();
            }

    }



    @Override
    public void onClick(final View view) {

        final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();

        switch (view.getId()) {
        case R.id.button_personal_list:
            return;

        case R.id.button_shared_list:
            return;


        case R.id.button_done:
            break;

        default:
            break;
    }

    activity.onBackPressed();


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



    @Override
    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.shared_list_toggle_control:
                mShouldbeon = isChecked;
                break;
            case R.id.personal_list_toggle_control:
                mShouldbeon = isChecked;
                break;

            default:
                break;
        }
        toggleDoneButton();
    }

Here's the corresponding xml : 这是相应的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/altercolor2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/altercolor2"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button_personal_list"
                android:layout_width="match_parent"
                android:layout_height="@dimen/manage_news_btn_ht"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/manage_market_category_btnbg"
                android:gravity="left|center_vertical"
                android:paddingLeft="@dimen/frequent_padding_left"
                android:text="@string/personal"
                android:textColor="#cccccc"
                android:textSize="@dimen/title" />



           <ToggleButton
        android:id="@+id/personal_list_toggle_control"
        style="@style/Button.Toggle"
        android:layout_width="@dimen/toggle_width_ht"
        android:layout_height="@dimen/toggle_width_ht"
        android:layout_alignParentRight="true"
         android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:paddingTop="0dp"
       />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button_shared_list"
                android:layout_width="match_parent"
                android:layout_height="@dimen/manage_news_btn_ht"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/manage_market_category_btnbg"
                android:gravity="left|center_vertical"
                android:paddingLeft="@dimen/frequent_padding_left"
                android:text="@string/shared"
                android:textColor="#cccccc"
                android:textSize="@dimen/title" />



           <ToggleButton
        android:id="@+id/shared_list_toggle_control"
        style="@style/Button.Toggle"
        android:layout_width="@dimen/toggle_width_ht"
        android:layout_height="@dimen/toggle_width_ht"
        android:layout_alignParentRight="true"
         android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:paddingTop="0dp"
       />

        </RelativeLayout>



    </LinearLayout>

</FrameLayout>

So, when personal lists is selected I want shared lists checkbox to be invisible and vice versa. 因此,当选择个人列表时,我希望共享列表复选框不可见,反之亦然。

They both extend CompoundButton so there isn't too much to it. 它们都extend CompoundButton,所以没有太多的东西。 Just put them inside of a RadioGroup and treat them like a RadioButton . 只需将它们放在RadioGroup ,并像RadioButton一样对待它们。

RadioGroup Docs RadioGroup文件

Not sure what else you need but I do it like this 不知道您还需要什么,但是我这样做

<RadioGroup android:id="@+id/typeGroup"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">
            <ToggleButton 
                android:id="@+id/hotBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="20dp"
                android:checked="true"
                style="@style/HotButton"
                android:onClick="filterItems"/>
            <ToggleButton 
                android:id="@+id/coldBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                style="@style/ColdButton"
                android:onClick="filterItems"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="40dp"
                android:layout_marginTop="20dp"/>
            <ToggleButton 
                android:id="@+id/frozenBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginRight="40dp"
                android:layout_marginTop="20dp"
                style="@style/FrozenButton"
                android:onClick="filterItems"/>
          </RadioGroup> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM