android RadioButton怎么设置默认选中

android RadioButton怎么设置默认选中,第1张

可以在布局文件中将RadioButton的checked属性设为ture,例如

 <RadioButton

            android:id="@+id/radioButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:checked="true"/>

或者在Java代码中调用radioButton的setchecked((true)。

Button 没有选中和不选中的状态,如果要选中或者不选中,要使用Android提供的RadioGroup+RadioButton 或者使用CheckBox这些控件。

RadioGroup+RadioButton Android会自动的选中,只会有一个选中。

CheckBox需要写状态去控制。

android 中的radiobutton点击2下才能选中的原因,因为一开始都有一个默认选中的item,类似于下面的代码:

for (int j = 0 j < newList.get(position).getList().size() j++) {

RadioButton radioButton = new RadioButton(context)

radioButton.setTextSize(9)  

radioButton.setText(newList.get(position).getList().get(j)

.get("dishname").toString())

radioButton.setTag(newList.get(position).getList().get(j)

.get("dishid").toString())

radioGroup.addView(radioButton, j)

if (j==0) {

radioButton.setCheck(true) 

}  

}

就是中给radioButton设置为选中,这样如果存在两个radiogroup就会出现点击两次才能选中的状态原因是:不需要设置RadioButton的默认选中, 这样会使RadioButton一直处于选中状态.

解决的方式:

应该给RadioGroup 设置选中的RadioButton   ,也就是说  

把  if (j==0) {

 radioButton.setCheck(true) 

}

更改为

if (j==0) {

radioGroup.check(radioButton.getId()) 

}  

遇到很多棘手的问题,建议查看api和接口方法的使用说明,很多官方的英文详细的列举了可能发生的问题。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11064239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存