Android学习笔记篇2. 单选按钮、复选按钮

这篇博客介绍了如何在Android XML布局文件中创建单选按钮(RadioGroup)和复选按钮(CheckBox)组,并通过Java代码实现监听事件,展示用户选择的性别和兴趣爱好。在单选按钮示例中,当用户点击性别选项时,底部文本会显示相应的性别信息。而在复选按钮部分,用户可以选择多个兴趣爱好,程序会动态更新文本,显示所选的爱好组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单选按钮

在XML里写一组单选按钮(2个)+ 一个文本(用于提示):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/rbtn"
            android:textSize="25dp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:textSize="25dp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>

    <TextView
        android:id="@+id/tv"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

在Java中编写代码

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rdg);
        textView = (TextView) findViewById(R.id.tv);
        /**
         * 利用setOnCheckedChangeListtener()为RadioGroup设置监听事件
         */
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                // 判断被点击的RadioButton
                if (i == R.id.rbtn) {
                    textView.setText("您的性别是:男");
                } else {
                    textView.setText("您的性别是:女");
                }
            }
        });
    }
}

运行,点击按钮,在底下提示相应信息:
在这里插入图片描述

复选按钮

在XML里写一组复选按钮 + 两个提示文本 + 一个显示输出文本

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择爱好:"
        android:textSize="18dp"/>

    <CheckBox
        android:id="@+id/shuttlecock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="羽毛球"
        android:textSize="18dp"/>
    <CheckBox
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"
        android:textSize="18dp"/>

    <CheckBox
        android:id="@+id/pingpong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球"
        android:textSize="18dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您选择的兴趣爱好为:"
        android:textSize="22dp"/>
    <TextView
        android:id="@+id/hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"/>

</LinearLayout>

在Java中编写代码,利用TextView提示哪一个复选框被选择:

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView hobby;
    private String hobbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 初始化CheckBox控件
         */
        CheckBox shuttlecock = (CheckBox) findViewById(R.id.shuttlecock);
        CheckBox basketball = (CheckBox) findViewById(R.id.basketball);
        CheckBox pingpong = (CheckBox) findViewById(R.id.pingpong);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        hobby = (TextView) findViewById(R.id.hobby);
        hobbys = new String();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();
        /**
         * 简易算法控制提示字符串
         */
        if (b) {
            if (!hobbys.contains(motion)) {
                hobbys = hobbys + motion;
                hobby.setText(hobbys);
            }
        } else {
            if (hobbys.contains(motion)) {
                hobbys = hobbys.replace(motion, "");
                hobby.setText(hobbys);
            }
        }
    }
}

运行,在底下提示对应信息:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莉妮可丝的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值