首页 > 其他分享 >AndroidStudio学习记录(4):单选按钮控件RadioButton

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

时间:2024-04-07 11:01:07浏览次数:15  
标签:控件 widget findViewById mySelection RadioButton 单选 import android id

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

<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);
            }
        }));
    }

标签:控件,widget,findViewById,mySelection,RadioButton,单选,import,android,id
From: https://blog.csdn.net/weixin_53406338/article/details/137456151

相关文章

  • WPF开发一个可以自适应排列的Panel控件
    一.控件介绍    初看标题可能无法理解,我们看看什么是自适应排列。乍一看它有点像WrapPanel控件,都是从左至右排列,如果一行排列不下就换行继续排列,但是细看你就会发现不对,WrapPanel控件行尾是不会对齐的,也就是说只要WrapPanel的子控件的宽度不一致,每一行的末尾就会必定留下一......
  • AndroidStudio学习记录(3):操纵按钮控件Botton、ImageBotton
    按钮控件是平时看到的,常用Botton和ImageButton控件,一般操纵按钮来实现相应的命令,比如在手机上的查找登录注册,以及点击命令等等。ImaBotton与Button的区别在于它没有文本,只有图片,需要制定图片路径在activity_main.xml文件中,它们是这样使用的:<?xmlversion="1.0"encoding=......
  • 【Qt】:常用控件(五:显示类控件)
    常用控件一.ProgressBar二.CalendarWidget一.ProgressBar使⽤QProgressBar表⽰⼀个进度条代码⽰例:设置进度条按时间增⻓设置定时器,每个0.1秒,让进度条+1在实际开发中,进度条的取值,往往是根据当前任务的实际进度来进行设置的。比如需要读取一个很大的文......
  • 【Qt】:常用控件(四:显示类控件)
    常用控件一.Lable二.LCDNumber一.LableQLabel可以⽤来显⽰⽂本和图⽚.代码⽰例:显⽰不同格式的⽂本代码⽰例:显⽰图⽚此时,如果拖动窗⼝⼤⼩,可以看到图⽚并不会随着窗⼝⼤⼩的改变⽽同步变化为了解决这个问题,可以在Widget中重写resizeEvent函数。......
  • 第三单元学校里所讲控件
    第三单元学校里所讲控件1.ImageView图片考点1:src和backgroundbackground是背景图片当设置长宽matchparentbackground会铺满而src不会,他会按原图的比例<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android&qu......
  • WPF中Ribbon控件的使用
    WPF中Ribbon控件的使用这篇博客将分享如何在WPF程序中使用Ribbon控件。Ribbon可以很大的提高软件的便捷性。上面截图使Outlook2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可以找到想要的Menu。在Outlook2003时代,将Home下面的Menu都垂直的排列下来,操作的便捷程......
  • 【WPF应用34】WPF基本控件-Menu的详解与示例
    WPF(WindowsPresentationFoundation)是.NET框架的一个部分,用于构建桌面应用程序的用户界面。在WPF中,菜单(Menu)是一种常用的控件,用于提供一组选项或命令,使用户可以根据自己的需要执行特定的操作。本文将详细介绍WPF中的Menu控件,包括其基本用法、属性和事件。同时,我们将通过一......
  • 【WPF应用35】深度解析WPF中的TreeView控件:功能、用法、特性与最佳实践
    WPF(WindowsPresentationFoundation)是微软推出的一个用于构建桌面应用程序的图形子系统。在WPF中,TreeView是一种常用的树形控件,用于显示层次结构的数据显示。本文将详细介绍WPF中的TreeView控件,并提供一个简单的示例。一、TreeView控件的基本概念TreeView控件用于显示一......
  • Qt自定义控件之Battery电池控件
    文章目录前言一、BasicBattery二、Battery控件三、效果总结前言在Qt应用程序开发中,自定义控件是一种常见的需求,开发者经常需要根据特定的需求创建定制化的控件来增强用户界面的交互性和美观性。Battery电池控件是一种常见的自定义控件,用于显示设备的电池状态。通过B......
  • playwright for net 对日期选择控件(My97DatePicker)的设置
     playwrightf对日期选择控件的设置,直接使用js脚本publicpartialclassMainForm:Form{IPlaywrightplaywright;IPagepage;publicMainForm(){InitializeComponent();}privateasyncvoidMainForm_Loa......