当前位置:首页 » 安卓系统 » androidradiogroup

androidradiogroup

发布时间: 2022-06-22 13:52:05

① 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怎么用

RadioButton和RadioGroup的关系:1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器2、每个RadioGroup中的RadioButton同时只能有一个被选中3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中4、大部分场合下,一个RadioGroup中至少有2个RadioButton5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置。

③ android的radiogroup怎么移动布局

实际上只要我们明白在radiogroup里面我们也可以使用RelativeLayout,LinearLayout这样的布局的;首先设置radiogroup的orientation属性为vertical

然后再第一个radiobutton前面加上LinearLayout,orientation属性设置为horizontal,</LinearLayout>标签放在一行最后一个radiobutton后面;小编这里是 文本为“50”的那个radiobutton后面

同样的把使用LinearLayout把后面几个radiobutton包裹住,orientation属性设置为horizontal,

运行一下就可以发现就达到了我们想要的结果!

④ android 中如何获取radiogroup 中那个radiobutton被选择

java">radiogroup本身有监听的方法可以直接设置监听,这个监听需要一个回调接口OnCheckedChangeListener,这个接口里面的回调方法给我们返回了两个参数其中int型的参数就是当前你选中的RadioButton的ID
radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
//checkId就是当前选中的RadioButton
}
});

⑤ android里RadioGroup的clearCheck()使用方法

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

01 RadioGroup相关属性:
02
03 RadioGroup.getCheckedRadioButtonId ();--获取选中按钮的id
04
05 RadioGroup.clearCheck ();//---清除选中状态
06
07 RadioGroup.check (int id);//---通过参入选项id来设置该选项为选中状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作
08
09 setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
10
11 addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局参数添加一个子视图
12
13 //参数 child 所要添加的子视图 index 将要添加子视图的位置 params 所要添加的子视图的布局参数
14
15 RadioButton.getText();//获取单选框的值
16
17 //此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件

RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

看案例:
1.定义布局文件:

01 <?xml version="1.0" encoding="utf-8"?>
02 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="match_parent"
04 android:layout_height="match_parent" >
05 <LinearLayout
06 android:orientation="vertical"
07 android:layout_width="match_parent"
08 android:layout_height="wrap_content"
09 android:layout_marginRight="5dp" >
10
11 <TextView
12 android:id="@+id/radiogroup_info_id"
13 android:layout_width="228px"
14 android:layout_height="wrap_content"
15 android:text="我选择的是...?"
16 android:textSize="30sp"
17 />
18
19 <RadioGroup
20 android:id="@+id/radioGroup_sex_id"
21 android:layout_width="match_parent"
22 android:layout_height="match_parent"
23 >
24 <RadioButton
25 android:id="@+id/boy_id"
26 android:layout_width="match_parent"
27 android:layout_height="match_parent"
28 android:text="Boy"
29 />
30 <RadioButton
31 android:id="@+id/girl_id"
32 android:layout_width="match_parent"
33 android:layout_height="match_parent"
34 android:text="Girl"
35 />
36
37 </RadioGroup>
38 <Button
39 android:id="@+id/radio_clear"
40 android:layout_width="match_parent"
41 android:layout_height="match_parent"
42 android:text="清除选中按钮"
43 />
44
45 <Button
46 android:id="@+id/radio_add_child"
47 android:layout_width="match_parent"
48 android:layout_height="match_parent"
49 android:text="添加单选项"
50 />
51
52 </LinearLayout>
53 </ScrollView>

2.java代码文件

01 package com.dream.app.start.first.radiobutton;
02
03 import com.dream.app.start.R;
04 import com.dream.app.start.R.id;
05 import com.dream.app.start.R.layout;
06 import com.dream.app.start.three.utils.PublicClass;
07
08 import android.app.Activity;
09 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.view.ViewGroup.LayoutParams;
13 import android.widget.Button;
14 import android.widget.RadioButton;
15 import android.widget.RadioGroup;
16 import android.widget.RadioGroup.OnCheckedChangeListener;
17 import android.widget.TextView;
18 import android.widget.ToggleButton;
19
20 public class RadioButtonDemo extends PublicClass {
21 private TextView textView=null;
22 private RadioGroup radioGroup=null;
23 private RadioButton radioButton_boy,radioButton_girl;
24 private Button radio_clear,child;
25 /* (non-Javadoc)
26 * <a href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a> android.app.Activity#onCreate(android.os.Bundle)
27 */
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 // TODO Auto-generated method stub
31 super.onCreate(savedInstanceState);
32
33 setContentView(R.layout.layout_frist_radiobuton);
34
35 textView = (TextView)findViewById(R.id.radiogroup_info_id);
36
37 //radioGroup
38 radioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);
39 radioButton_boy=(RadioButton)findViewById(R.id.boy_id);
40 radioButton_girl=(RadioButton)findViewById(R.id.girl_id);
41 child=(Button)findViewById(R.id.radio_add_child);
42 //---
43 radioGroup.setOnCheckedChangeListener(listen);
44 radio_clear=(Button)findViewById(R.id.radio_clear);
45 radio_clear.setOnClickListener(onClick);
46 child.setOnClickListener(onClick);
47 }
48
49 private OnCheckedChangeListener listen=new OnCheckedChangeListener() {
50
51 @Override
52 public void onCheckedChanged(RadioGroup group, int checkedId) {
53 int id= group.getCheckedRadioButtonId();
54 switch (group.getCheckedRadioButtonId()) {
55 case R.id.girl_id:
56 textView.setText("我选择的是:"+radioButton_girl.getText());
57 break;
58 case R.id.boy_id:
59 textView.setText("我选择的是:"+radioButton_boy.getText());
60 break;
61 default:
62 textView.setText("我选择的是:新增");
63 break;
64 }
65
66 }
67 };
68 private OnClickListener onClick=new OnClickListener() {
69
70 @Override
71 public void onClick(View v) {
72 radio_clear=(Button)v;
73 switch (radio_clear.getId()) {
74 case R.id.radio_clear:
75 radioGroup.check(-1);//清除选项
76 // radioGroup.clearCheck(); //清除选项
77 textView.setText("我选择的是...?");
78 break;
79 case R.id.radio_add_child:
80 //新增子
81 RadioButton newRadio =new RadioButton(getApplicationContext());
82 newRadio.setText("新增");
83 radioGroup.addView(newRadio, radioGroup.getChildCount());
84 break;
85 //
86
87 default:
88 break;
89 }
90 }
91 };

⑥ 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单选 怎么处理不选择的情况

设置一个默认值,让页面一开始加载的时候就有一个选项,这样就没有不选择的情况了。
mRbMonth.setChecked(true);
或在配置文件里加一个属性:android:checked = "true"
都可以。

⑧ android开发radiogroup怎么添加其他的控件

radiogroup只能添加radiogbutton哈,是配套使用的控件。

⑨ 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中的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

热点内容
魔兽世界自动钓鱼脚本 发布:2024-05-19 06:43:07 浏览:494
cbs加密 发布:2024-05-19 06:29:56 浏览:200
ssis存储过程 发布:2024-05-19 06:21:31 浏览:630
怎样删除小视频文件夹 发布:2024-05-19 05:49:29 浏览:589
开启php短标签 发布:2024-05-19 05:44:12 浏览:473
android各国语言 发布:2024-05-19 05:42:54 浏览:247
微信什么资料都没怎么找回密码 发布:2024-05-19 05:35:34 浏览:907
填志愿密码是什么 发布:2024-05-19 05:30:23 浏览:318
城堡争霸自动掠夺脚本 发布:2024-05-19 05:22:06 浏览:204
asp编程工具 发布:2024-05-19 05:20:36 浏览:143