安卓编程中的CheckBox和RadioButton(纯小白自学嘻嘻)

本文详细介绍了安卓开发中CheckBox和RadioButton的使用方法。CheckBox适用于多选场景,通过setOnClickListener监听选中状态;RadioButton则用于单选,常与RadioGroup配合使用。文章通过实例代码展示了如何设置文本、监听事件及响应。

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

安卓编程中的CheckBox和RadioButton(纯小白自学嘻嘻)

CheckBox:

			CheckBox是同时可以选择多个选项

如何判断CheckBox被选中,选中后如何触发事件

final CheckBox checkBox1 = findViewById(R.id.checkBox1);
checkBox1.setText(“判断是否点击”); //首先将checkBox1的文本内容设置为判断是否点击
checkBox1.setOnClickListener(new View.OnClickListener() {//调用CheckBox的方法setOnClickListener检测是否选择框是否被选中
@Override
public void onClick(View v) {//重写选中触发事件
textViewOfCB1.setText(“secleted:” + checkBox1.isChecked());//若被选中,触发事件:将checkBox1后对用的TextView内容换成secleted: true
}
在这里插入图片描述
在这里插入图片描述

RadioButton

			RadioButton则是仅可以选择一个选项的控件
			RadioGroup是RadioButton的承载体,程序运行时不可见,一个RadioGroup可以包含多个RadioButton,在每一个RadioGroup中,用户仅能够选择其中一个RadioButton
			RadioGroup在设计时可见,运行时不可见
			
			向RadioGroup中放入两个RadioButton时,默认为vertical(垂直摆放)

在这里插入图片描述的
在这里插入图片描述
选中ButtonGroup在Attribute栏中找到orientation,并设置为horizontal即可变为水平模式
在这里插入图片描述

小练习

在这里插入图片描述
在这里插入图片描述

代码:

package com.example.practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textViewOfBasicInfo = findViewById(R.id.textViewOfBasicInfo);
    textViewOfBasicInfo.setText("基本信息");

    TextView textViewOfName = findViewById(R.id.textVieOfName);
    textViewOfName.setText("姓名:");

    TextView textViewOfHeight = findViewById(R.id.textViewOfHeight);
    textViewOfHeight.setText("身高:");

    TextView textViewOfWeight = findViewById(R.id.textViewOfWeight);
    textViewOfWeight.setText("体重");

    TextView textViewOfCm = findViewById(R.id.textViewOfCm);
    textViewOfCm.setText("CM");

    TextView textViewOfKg = findViewById(R.id.textViewOfKg);
    textViewOfKg.setText("KG");

    TextView textViewOfGender = findViewById(R.id.textViewOfGender);
    textViewOfGender.setText("性别:");

    TextView textViewOfHobby = findViewById(R.id.textViewOfHobby);
    textViewOfGender.setText("爱好:");

    RadioButton radioButtonOfMan = findViewById(R.id.radioButtonOfMan);
    radioButtonOfMan.setText("男");

    RadioButton radioButtonOfLady = findViewById(R.id.radioButtonOfLady);
    radioButtonOfLady.setText("女");

    CheckBox checkBoxOfHobbyOfTravel = findViewById(R.id.checkBoxOfHobbyOfTravel);
    checkBoxOfHobbyOfTravel.setText("旅游");

    final CheckBox checkBoxOfHobbyOfRun = findViewById(R.id.checkBoxOfHobbyOfRun);
    checkBoxOfHobbyOfRun.setText("运动");

    CheckBox checkBoxOfHobbyOfTOther = findViewById(R.id.checkBoxOfHobbyOfOther);
    checkBoxOfHobbyOfTOther.setText("其他");

    final TextView textViewOfOut = findViewById(R.id.textViewOfOut);
    textViewOfOut.setText("你的爱好:");

    checkBoxOfHobbyOfRun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBoxOfHobbyOfRun.isChecked()) {
                textViewOfOut.setText("你的爱好是运动");
            }
        }
    });





}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值