gpt4 book ai didi

android - setChecked 单选按钮无法正常工作

转载 作者:行者123 更新时间:2023-11-29 01:14:28 25 4
gpt4 key购买 nike

我想让我的 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>

最佳答案

我上面的代码是正确的。问题是该 fragment 是我不了解 fragment 类的体系结构。

关于android - setChecked 单选按钮无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com