① android的radiogroup为什么选择两个
项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . ..
我开始这样给设置默认选中一个的:
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设置为选中.. .
网上查找了下类似的情况 如 这篇文章 ,都没有解决我的问题.
最后研究了下 android 官方Api 和部分 RadioGroup的源代码 后发现. 其实很简单
我们不需要设置RadioButton的默认选中, 这样会使RadioButton一直处于选中状态.
我们应该给RadioGroup 设置选中的RadioButton ,也就是说
把 if (j==0) {
radioButton.setCheck(true);
}
更改为
if (j==0) {
radioGroup.check(radioButton.getId());
}
轻松搞定.. 哎呦了个去,官方Api和源码是个好东西啊.
② android RadioGroup问题
OnCheckedChangeListener是一个接口,不是基类。
在android中,所有的*Listener都是接口。
用implements,试试
class OnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener
③ Android中的radiogroup中的radioButton都不可编辑
radiobutton radiobutton = new radiobutton(this);
radiogroup.layoutparams layoutparams = new radiogroup.layoutparams(radiogroup.layoutparams.wrap_content,50);
layoutparams.setmargins(10, 10, 10, 10);
radiobutton.setlayoutparams(layoutparams);
radiobutton.settext("rb1");
radiobutton.settextsize(12);
radiobutton.setgravity(gravity.center);
radiobutton.setpadding(10, 10, 10, 10);
radiogroup.addview(radiobutton);
//上面是添加1个,自己写个循环添加吧。
//上面那个layoutparams必须是radiogroup的,因为radiobutton要添加的容器是radiogroup
④ Android如何动态生成Radio和RadioGroup
privateLinearLayoutlayout;//布局,可以在xml布局中获得
privateRadioGroupgroup;//点选按钮组
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
layout=newLinearLayout(this);//实例化布局对象
group=newRadioGroup(this);//实例化单选按钮组
//添加单选按钮
for(inti=0;i<5;i++){
RadioButtonradio=newRadioButton(this);
radio.setText("radio"+i);
group.addView(radio);
}
//将单选按钮组添加到布局中
layout.addView(group);
this.setContentView(layout);
}
可以把单选按钮组放在ScrollView中,这样的话,多出的部分可以滚动查看了。
⑤ android的radiogroup怎么移动布局
实际上只要我们明白在radiogroup里面我们也可以使用RelativeLayout,LinearLayout这样的布局的;首先设置radiogroup的orientation属性为vertical
然后再第一个radiobutton前面加上LinearLayout,orientation属性设置为horizontal,</LinearLayout>标签放在一行最后一个radiobutton后面;小编这里是 文本为“50”的那个radiobutton后面
同样的把使用LinearLayout把后面几个radiobutton包裹住,orientation属性设置为horizontal,
运行一下就可以发现就达到了我们想要的结果!
⑥ android radiogroup怎么用
RadioButton和RadioGroup的关系:1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器2、每个RadioGroup中的RadioButton同时只能有一个被选中3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中4、大部分场合下,一个RadioGroup中至少有2个RadioButton5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置。
⑦ android如何实现代码控制RadioGroup中某一个按钮选中
RadioButton在做表单的时候经常用到,在安卓开发中,RadioButton需要和RadioGroup一起使用,表示在一组可选项中,只有一
个可以被选中,RadioGroup状态改变的一个监视器OnCheckedChangeListener,RadioGroup使用的时候调用
setOnCheckedChangeListener(),然后重写OnCheckedChangeListener中的
onCheckedChanged()方法,比如:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 获取变更后的选项的ID
int radioButtonId = group.getCheckedRadioButtonId();
switch (radioButtonId) {
case R.id.message_radiobtn:
mFragment = new MessageFragment();
break;
case R.id.contact_radiobtn:
mFragment = new ContactFragment();
break;
case R.id.dynamic_radiobtn:
mFragment = new DynamicFragment();
break;
default:
break;
}
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.realtabcontent, mFragment).commit();
}
});这篇简单写了一个几行代码介绍,实现的效果有点类似QQ底部导航切换,Teachcourse博客:
⑧ android开发中 关于radiogroup
在radiogroup的xml文件里面设置 android:orientation="horizontal"这个属性就可以了。
⑨ android 中如何获取radiogroup 中那个radiobutton被选择
java">radiogroup本身有监听的方法可以直接设置监听,这个监听需要一个回调接口OnCheckedChangeListener,这个接口里面的回调方法给我们返回了两个参数其中int型的参数就是当前你选中的RadioButton的ID
radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
//checkId就是当前选中的RadioButton
}
});
⑩ android radiogroup怎么初始化按钮
请说明完整你的问题。
RadioButton carButton = (RadioButton) findViewById(R.id.search_car);
RadioButton busButton = (RadioButton) findViewById(R.id.search_bus);
RadioButton walkButton = (RadioButton) findViewById(R.id.search_walk);
以上是三个button的初始化,也就是找到改按钮。
如果需要设置某个按钮是被选中的
请调用performClick();方法
例如:
carButton.performClick();