2016-11-21 2 views
2

Я бы хотел, чтобы у моего приложения Android была установлена ​​радиокнопка с помощью оператора switch. Я подтвердил, что передаваемая строка соответствует одному из случаев, и все еще ничего не проверяется. Что я делаю неправильно?setChecked Radio Button не работает должным образом

 radioGroup = (RadioGroup) v.findViewById(R.id.gender); 
     radioGroup2 = (RadioGroup) v.findViewById(R.id.year); 
     fre = (RadioButton) v.findViewById(R.id.freshmen); 
     soph = (RadioButton) v.findViewById(R.id.sophomore); 
     jun = (RadioButton) v.findViewById(R.id.junior); 
     sen = (RadioButton) v.findViewById(R.id.senior); 
     male = (RadioButton) v.findViewById(R.id.radioButton); 
     female = (RadioButton) v.findViewById(R.id.radioButton2); 
     lgbtq = (RadioButton) v.findViewById(R.id.radioButton3); 

     switch (global.getGender()) { 
      case "Male": male.setChecked(true); 
       break; 
      case "Female": female.setChecked(true); 
       break; 
      case "LGBTQ": lgbtq.setChecked(true); 
       break; 

     } 
     switch (global.getYear()) { 
      case "Fresh": fre.setChecked(true); 
       break; 
      case "Soph": soph.setChecked(true); 
       break; 
      case "Junior": jun.setChecked(true); 
       break; 
      case "Senior": sen.setChecked(true); 
       break; 
     } 

и в XML

<RadioGroup 
    android:id="@+id/year" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_above="@+id/bio" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 
    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fresh" 
     android:id="@+id/freshmen" 
     android:layout_below="@+id/editText8" 
     android:buttonTint="#00F2EA"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Soph " 
     android:buttonTint="#009B40" 
     android:id="@+id/sophomore" 
     /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Junior" 
     android:buttonTint="#FF0000" 
     android:id="@+id/junior" 
     /> 
    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Senior" 
     android:buttonTint="#000" 
     android:id="@+id/senior" 
     /> 
</RadioGroup> 

<RadioGroup 
    android:id="@+id/gender" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_above="@+id/year" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 
    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Male " 
     android:id="@+id/radioButton" 
     android:layout_below="@+id/editText8" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:buttonTint="#076EB9"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Female" 
     android:buttonTint="#FF00AA" 

     android:id="@+id/radioButton2" 
     android:layout_alignTop="@+id/radioButton" 
     android:layout_toRightOf="@+id/radioButton" 
     android:layout_toEndOf="@+id/radioButton" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="LGBTQ" 
     android:buttonTint="#bf00ff" 
     android:id="@+id/radioButton3" 
     android:layout_alignTop="@+id/radioButton2" 
     android:layout_toRightOf="@+id/radioButton2" 
     android:layout_toEndOf="@+id/radioButton2" /> 
</RadioGroup> 

ответ

1

Мой код выше был правильным. Проблема заключалась в том, что фрагмент был тем, что я не понял архитектуру класса фрагмента.

Смежные вопросы