posts - 296,comments - 1,views - 2958

一.所花时间

0.5h

二.代码量

40行

三.博客量

1篇

四.了解到的知识点

单选按钮RadioButton

所谓单选按钮,指的是在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范
围,这个容器便是单选组RadioGroup。单选组实质上是个布局,同一组RadioButton都要放在同一个
RadioGroup节点下。RadioGroup提供了orientation属性指定下级控件的排列方向,该属性为
horizontal时,单选按钮在水平方向排列;该属性为vertical时,单选按钮在垂直方向排列。
RadioGroup下面除了RadioButton,还可以挂载其他子控件(如TextView、ImageView等)。如此看
来,单选组相当于特殊的线性布局,它们主要有以下两个区别:
(1)单选组多了管理单选按钮的功能,而线性布局不具备该功能。
(2)如果不指定orientation属性,那么单选组默认垂直排列,而线性布局默认水平排列。
下面是RadioGroup在Java代码中的3个常用方法。
check:选中指定资源编号的单选按钮。
getCheckedRadioButtonId:获取已选中单选按钮的资源编号。
setOnCheckedChangeListener:设置单选按钮勾选变化的监听器。
与CheckBox不同的是,RadioButton默认未选中,点击后显示选中,但是再次点击不会取消选中。只有点击同组的其他单选按钮时,原来选中的单选按钮才会取消选中。另需注意,单选按钮的选中事件是
由RadioButton处理,而是由RadioGroup处理。 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择您的性别"
        android:textColor="@color/black"
        android:textSize="17sp" />
    <RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:text="男"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:text="女"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RadioGroup>
    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="17sp" />
 </LinearLayout>

接着编写对应的Java代码,主要是如何处理选中监听器,

public class RadioHorizontalActivity extends AppCompatActivity
 implements RadioGroup.OnCheckedChangeListener {
 private TextView tv_sex; // 声明一个文本视图对象
@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_radio_horizontal);
 // 从布局文件中获取名叫tv_sex的文本视图
tv_sex = findViewById(R.id.tv_sex);
 // 从布局文件中获取名叫rg_sex的单选组
RadioGroup rg_sex = findViewById(R.id.rg_sex);
 // 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
rg_sex.setOnCheckedChangeListener(this);
 }
 // 在用户点击组内的单选按钮时触发
@Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 if (checkedId == R.id.rb_male) {
 tv_sex.setText("哇哦,你是个帅气的男孩");
 } else if (checkedId == R.id.rb_female) {
 tv_sex.setText("哇哦,你是个漂亮的女孩");
 }
 }
 }

 

posted on   leapss  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2023-05-11 每日打卡-21.4
2023-05-11 每日打卡-21.3
2023-05-11 每日打卡-21.2
2023-05-11 每日打卡-21.1
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示