RadioButton,就是一个单选按钮,Radioroup包含一组RadioButton,供选择,这里只能单选,并且选中后再次点击不会取消选中。

常用属性:

android:checked="true" 设置 是否为选中状态

android:text="@string/title1_button2"设置单选的文字内容

android:layout_marginLeft="30dp"控制两个单选按钮之间的距离

android:paddingLeft="20dp" 控制单选按钮和文字之间的距离

android:button="@drawable/radio" 设置单选按钮的点击效果

布局文件如下:

   <RadioGroup         android:id="@+id/title1_group"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="horizontal"        android:layout_marginLeft="10dp">          <RadioButton              android:id="@+id/title1_button1"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="@string/title1_button1"               android:textColor="@color/description_text_color"               android:button="@drawable/radio"               android:paddingLeft="20dp"                android:textSize="@dimen/title_text_size"                     />            <RadioButton              android:id="@+id/title1_button2"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="@string/title1_button2"               android:textColor="@color/description_text_color"               android:textSize="@dimen/title_text_size"             android:button="@drawable/radio"                android:paddingLeft="20dp"             android:layout_marginLeft="30dp" 设置和上一个单选按钮的左边距离30dp         />          </RadioGroup>   


在代码中添加方式如下:

                                RadioButton radioBtn1, radioBtn2;                          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);params.gravity = Gravity.CENTER_VERTICAL;                  RadioGroup radioGroup = new RadioGroup(this);         radioGroup.setLayoutParams(params);radioGroup.setOrientation(RadioGroup.HORIZONTAL);radioBtn1 = new RadioButton(this);// 界面布局宽度,高度LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);radioBtn1.setLayoutParams(lp1);radioBtn1.setText("按钮1");         radioBtn1.setTextColor(Color.parseColor("#999d9c"));radioGroup.addView(radioBtn1);radioBtn2 = new RadioButton(this);LinearLayout.LayoutParams lp2= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);radioBtn2.setLayoutParams(lp2);                  radioBtn2.setTextColor(Color.parseColor("#999d9c"));radioBtn2.setText("按钮2");radioGroup.addView(radioBtn2);


设置RadioGroup的监听

                  title1_group = (RadioGroup) findViewById(R.id.title1_group);title1_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {title1_group_isSelected = true;if (checkedId == R.id.title1_button1) {title1_group_selected = 0;} else if (checkedId == R.id.title1_button2) {title1_group_selected = 1;}}});


更多相关文章

  1. 【android】点击事件穿透解决方案
  2. 二级菜单---ExpandableListView
  3. Android(安卓)UI编程基础3
  4. Android跟软键盘的故事
  5. Android(安卓)P 系统设置默认值
  6. Xamarin 中开发Android实现全屏或者不显示标题栏的方法-宋兴柱
  7. [置顶] Android中万能适配器
  8. Android开发EditText属性
  9. Http的15种请求

随机推荐

  1. adb devices 找不到设备的解决方法
  2. android 循环滚动实现
  3. Android中的HTTP通信
  4. Android下打开/关闭wifi 及 状态(5个)的
  5. Ubuntu 17.10 Android(安卓)studio 3.1
  6. QT5.2 Android(安卓)App找不到插件qmlxml
  7. android:动态申请权限(一)
  8. 关闭Android开机锁屏
  9. seekBard的使用方法
  10. Android P按键静音流程