【1】RadioButton搭配RadioGroup使用html
【1】简单的男女选择Demo 这里是本身写死了就只有男、女的2种选择 <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="性别" > <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" > </RadioButton> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" > </RadioButton> </RadioGroup> 【2】 稍稍复杂一点的,只是写一个容器,单个的RadioGroup的并不会在布局文件中写 <RadioGroup android:id="@+id/time_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > </RadioGroup>
Java部分代码、 // 时间列表 private RadioGroup indicator; for (int i = 0; i < 7; i++) { //Java代码中自定义RadioButton的UI式样 RadioButton btn = new RadioButton(mContext); //设置触摸效果 btn.setBackgroundResource(R.drawable.week_item_bg); btn.setButtonDrawable(new BitmapDrawable()); btn.setText("......"); Resources resource = getBaseContext().getResources(); //对于不一样的按钮状态,采用不一样的颜色显示文字:能够学习 ColorStateList csl = (ColorStateList) resource .getColorStateList(R.color.week_item_textcolor); if (csl != null) { btn.setTextColor(csl); } //设置监听事件 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { } }); //在Java代码中设置RadioGroup的一些属性:须要学习一下LayoutParams的使用方法 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); params.weight = 1; params.topMargin = 2; params.bottomMargin = 2; //继续设置单个RadioButton的属性 btn.setGravity(Gravity.CENTER); btn.setLayoutParams(params); //将定义好的RadioButton添加到RadioGroup indicator.addView(btn);
参考网址
java
http://blog.csdn.net/ganlijianstyle/article/details/7593812android
【2】经常使用方法属性ide
1)android:checked=“”函数
2)监听方法:布局
setOnCheckedChangeListener()
3)得到checked项目:学习
getCheckedRadioButtonId()
4)demothis
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup); //绑定一个匿名监听器 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub //获取变动后的选中项的ID int radioButtonId = arg0.getCheckedRadioButtonId(); //根据ID获取RadioButton的实例 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId); //更新文本内容,以符合选中项 tv.setText("您的性别是:" + rb.getText()); } });
5)补充.net
RadioGroup.getCheckedRadioButtonId ();--获取选中按钮的id RadioGroup.clearCheck ();//---清除选中状态 RadioGroup.check (int id);//---经过参入选项id来设置该选项为选中状态若是传递-1做为指定的选择标识符来清除单选按钮组的勾选状态,至关于调用clearCheck()操做 setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数 addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局参数添加一个子视图 //参数 child 所要添加的子视图 index 将要添加子视图的位置 params 所要添加的子视图的布局参数 RadioButton.getText();//获取单选框的值 //此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件