gpt4 book ai didi

android - getCheckedradiobutton 总是返回 -1

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

这是我的代码

final RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb1 = (RadioButton) findViewById(R.id.radio0);
RadioButton rb2 = (RadioButton) findViewById(R.id.radio1);
RadioButton rb3 = (RadioButton) findViewById(R.id.radio2);
final CheckBox cb1 = (CheckBox) findViewById(R.id.chkbox1);
int id = rg1.getCheckedRadioButtonId();
System.out.println("------------------------|"+id);
switch (id) {
case R.id.radio0:
cb1.setEnabled(true);
break;

case R.id.radio1:
cb1.setEnabled(true);
break;

case R.id.radio2:
cb1.setEnabled(true);
break;

default:
cb1.setEnabled(false);
break;
}

这总是返回 -1(chkbox 总是禁用),我似乎无法让它工作。另外,我还尝试通过 setId 将单个值分配给单选按钮,但它也不起作用。

这是我的 XML

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
android:checkedButton ="1"
/>

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
android:checkedButton="2"
/>

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3"
android:checkedButton="3"
/>
</RadioGroup>

最佳答案

您在错误的 View 上使用了“android:checkedButton”属性。当您将此属性添加到每个单选按钮时,此属性标识哪个单选按钮是一组被选中。尝试将其更改为以下内容

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:checkedButton="2"
>

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
/>

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
/>

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3"
/>

关于android - getCheckedradiobutton 总是返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16515493/

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