我的代码中有2个切换按钮,我想改变行为,这样当选择一个时,另一个不是,反之亦然。 (希望它像单选按钮一样工作)。任何关于我如何去做的想法? 这是我的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();
}
这是相应的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>
因此,当选择个人列表时,我希望共享列表复选框不可见,反之亦然。
答案 0 :(得分:3)
他们extend
CompoundButton所以没有太多内容。只需将它们放在RadioGroup
内,并将它们视为RadioButton
。
不确定你需要什么,但我这样做
<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>