AndroidStudio学习记录(4):单选按钮控件RadioButton

用于应用二选一等多选选项的设置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<!--单选按钮;RadioButton;性别 二选一;做题的4选一;含有选择按钮的时候用到这个单选按钮
    有选中和未选中两种状态;
    checked         选中和未选中
    OnCheckedChangeListener     设置状态转化监听事件
    -->
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="性别"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg_gender">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:layout_marginLeft="20dp"
                android:checked="true"
                android:id="@+id/rb_male"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:layout_marginLeft="20dp"
                android:id="@+id/rb_female"/>
    </RadioGroup>

</LinearLayout>

此处是运行效果:

下面是一个具体的实例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="ExtraText">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="你的职业" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="教师" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="学生" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="你的爱好" />

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="游泳" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:text="足球" />
    </RadioGroup>

    <Space
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:lines="3"
        android:text="请选择结果"
        android:id="@+id/tv02"/>

    <Space
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定" />


</LinearLayout>

下面是后台代码:

package com.aaa.radpro;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //1、声明变量    TextView textView02;
    RadioGroup myChoice;
    Button btn01;

    @SuppressLint("MissingInflatedId")
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2、此处给变量进行赋值;        textView02=(TextView) findViewById(R.id.tv02);//最好二者的名字一致;        myChoice=(RadioGroup) findViewById(R.id.radioGroup2);//第二个按钮组名        btn01=(Button) findViewById(R.id.btn01);//        //3、事件;分别是按钮事件3.1;爱好变更事件3.2        btn01.setOnClickListener((new View.OnClickListener() {
            @Override            public void onClick(View v) {
                String mySelection="";
                //定义一个变量来获取一个单选按钮对象                RadioButton radioButton01=(RadioButton) findViewById(R.id.radioButton1);
                //第一个选择,则第二个选择没有做出,第一个没选择。第二个做选择                if(radioButton01.isChecked())
                    mySelection="你的职业是:职业";
                else                    mySelection="你的职业是学生";
                //将结果设置到第二个文本显示区域                textView02.setText(mySelection);
            }
        }));
    }

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值