问题
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. (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:
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 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.
回答1:
They both extend
CompoundButton so there isn't too much to it. Just put them inside of a RadioGroup
and treat them like a RadioButton
.
RadioGroup Docs
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>
来源:https://stackoverflow.com/questions/22186523/how-do-i-make-my-toggle-button-act-like-a-radio-button