setChecked 单选按钮无法正常工作
setChecked Radio Button not working properly
我想让我的 android 应用程序使用 switch 语句检查单选按钮。我已经确认传递的字符串与其中一种情况相匹配,但仍然没有检查任何内容。我做错了什么?
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>
我上面的代码是正确的。问题是片段是我不理解片段的架构class。
我想让我的 android 应用程序使用 switch 语句检查单选按钮。我已经确认传递的字符串与其中一种情况相匹配,但仍然没有检查任何内容。我做错了什么?
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>
我上面的代码是正确的。问题是片段是我不理解片段的架构class。