0

내 코드에 2 개의 토글 버튼이 있으며, 하나를 선택하면 다른 하나는 선택되지 않으며 그 반대도 마찬가지입니다. (라디오 버튼처럼 작동하게하고 싶다.) 내가 어떻게 할 수 있는지에 대한 아이디어는? 토글 버튼을 라디오 버튼처럼 작동 시키려면 어떻게합니까?

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> 

그래서, 개인 목록은 내가 공유 목록의 체크 박스가 눈에 보이지 않는 그 반대가되고 싶어요를 선택하면.

답변

3

둘 다 extendCompoundButton이므로 너무 많지 않습니다. 그냥 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> 
+0

처럼 그것을 다른 무엇

RadioGroup Docs

확실하지, 현재 내가 별도로, radiogroups을 사용하려고 그들을은 LinearLayouts에있다 ,하지만 예상대로 작동하지 않았다 –

+0

방금'RadioGroup extends LinearLayout'을 게시 했으므로'LinearLayout'은 필요하지 않지만'RadioButton'처럼 부모 'RadioGroup' 안에 있어야합니다. 궁극적으로 원하는 이미지가 없으면'layout'을 설정하는 방법을 말할 수는 없지만 내 생각에는 – codeMagic

+0

내 경우에는 < 토글 버튼 /> ?? –

관련 문제