当前位置: 首页 > ai >正文

获取RadioButton的text,更换textview的text

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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="24dp">

    <RadioGroup
        android:id="@+id/gender_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/radio_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <TextView
        android:id="@+id/result_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别"
        android:textSize="18sp"
        android:layout_marginTop="20dp" />
</LinearLayout>

java代码:

package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private RadioGroup genderGroup;

    private TextView resultText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        genderGroup = findViewById(R.id.gender_group);
        resultText = findViewById(R.id.result_text);

        genderGroup.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton selected = findViewById(checkedId);
            String gender = selected.getText().toString();
            resultText.setText("你选择的是:" + gender);
        });
    }
}

运行结果:

心得:

1、genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {

    }
});

代码与

2、genderGroup.setOnCheckedChangeListener((group, checkedId) -> {

})

代码虽然作用一样android studio的编译器不会报错,但1的模拟器却运行不了,只能用2

==

android studio版本android-studio-2023.3.1.19-windows出现问题

格式化代码:ctrl+alt+L

http://www.xdnf.cn/news/13356.html

相关文章:

  • C++笔记-C++11(二)
  • 【Unity优化】提高热更新和打包速度
  • Centos与RockLinux设置静态ip
  • 数据库管理与高可用-PostgreSQL日常维护
  • MongoDB入门指南:环境安装与基本操作
  • QGIS新手教程4:相交、缓冲区与合并操作详解(含实战案例)
  • 多头与空头:市场博弈的两面
  • 【2025最新】Adobe Illustrator下载保姆级安装教程(附官方下载链接)
  • ThinkPad 交换 Ctrl 键和 Fn 键
  • Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘xxx’)
  • RK3588开发笔记-GNSS-RTK模块调试
  • YOLO-World 部署踩坑记录
  • Linux611 libvirtb ;FTP vsftpd.conf部分配置文件
  • React 元素的生命周期
  • 从硬件视角审视Web3安全:CertiK CTO主持Proof of Talk圆桌论坛
  • GO 入门小项目-博客-结合Gin Gorm
  • 【面板数据】中国与世界各国农产品进出口贸易数据(2015-2024年)
  • 国内外数据要素标准有何不同?
  • K8S项目需求分析
  • 火山引擎发布豆包大模型 1.6 与视频生成模型 Seedance 1.0 pro
  • ABP vNext + Spark on Hadoop:实时流处理与微服务融合
  • 嵌入式学习笔记 - C语言访问地址的方式,以及指针的进一步理解
  • JMeter 处理 UTF-16 转 UTF-8 乱码问题解决方案(deepseek)
  • AnythingLLM配置Milvus后,上传文档提示向量数据库标识符错误的解决办法
  • 鹰盾Win播放器作为专业的视频安全解决方案,除了硬件翻录外还有什么呢?
  • 微信小程序分享带参数地址
  • UFS-Ver3.1-第八章
  • 6.11 打卡
  • 对话机器人预测场景与 Prompt / 模型选择指南
  • 探究:什么是扁平化组织?有什么益处?