• Android学习笔记 2.3.5 RadioButton和CheckBox && 2.3.6 ToggleButton和Switch的功能和用法


    Android学习笔记

    疯狂Android讲义

    第2章 Android 应用的界面编程

    2.3 第2组 UI组件:TextView及其子类

    2.3.5 单选按钮(RadioButton)和复选框(CheckBox)的功能与用法

    单选钮(RadioButton)、复选框(CheckBox)、状态开关按钮(ToggleButton)和开关(Switch)是用户界面中最普通的UI组件,它们都继承了Button类,因此都可直接使用Button支持的各种属性和方法。

    RadioButton,CheckBox与普通按钮不同的是,它们多了一个可选中的功能,因此RadioButton,CheckBox都可额外指定一个android:checked属性,该属性用于指定RadioButton、CheckBox初始时是否被选中。

    RadioButton与 CheckBox的不同之处在于,一组RadioButton只能选中其中一个,因此RadioButton通常要与RadioGroup一起使用,用于定义一组单选钮。

    实例——利用单选按钮、复选框获取用户信息

    在这里插入图片描述

    定义布局

    
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="12dp">
    
        <TableRow android:gravity="center_vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别:" />
    
            
            <RadioGroup
                android:id="@+id/rg"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal">
                
                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="" />
    
                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />
    
            RadioGroup>
    
        TableRow>
    
        <TableRow android:gravity="center_vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="喜欢的颜色:" />
    
            
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">
                
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="红色" />
    
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="蓝色" />
    
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="绿色" />
    
            LinearLayout>
    
        TableRow>
    
        <TextView
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    
    TableLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    在这里插入图片描述

    逻辑代码

    package com.dingjiaxiong.checkbuttontest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            //获取界面上的组件
            RadioGroup rg = findViewById(R.id.rg);
            TextView show = findViewById(R.id.show);
            //为RadioGroup组件的点击事件绑定
            rg.setOnCheckedChangeListener((group, checkedId) -> {
                String tip = checkedId == R.id.male ? "您的性别是男" : "您的性别是女";
                show.setText(tip);
            });
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    运行效果

    在这里插入图片描述

    2.3.6 状态开关按钮(ToggleButton)和开关(Switch)的功能和用法

    状态开关按钮(ToggleButton)和开关(Switch)也是由Button派生出来的,因此它们的本质也是按钮,Button支持的各种属性、方法也适用于ToggleButton和 Switch。从功能上来看,ToggleButton .Switch与CheckBox复选框非常相似,它们都可以提供两种状态。不过 ToggleButton、Switch与CheckBox的区别主要体现在功能上,ToggleButton、Switch通常用于切换程序中的某种状态。

    ToggleButton支持的XML属性及相关方法:

    在这里插入图片描述

    Switch支持的XML属性及方法:

    在这里插入图片描述

    在这里插入图片描述

    实例——动态控制布局

    创建一个新模块

    在这里插入图片描述

    布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        
        <ToggleButton
            android:id="@+id/toggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="横向排列"
            android:textOn="纵向排列" />
    
        <Switch
            android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="横向排列"
            android:textOn="纵向排列"
            android:thumb="@drawable/check" />
    
        
        <LinearLayout
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical">
    
            
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮1" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮2" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮3" />
    
    
        LinearLayout>
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    逻辑代码

    package com.dingjiaxiong.togglebuttontest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.LinearLayout;
    import android.widget.Switch;
    import android.widget.ToggleButton;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ToggleButton toggle = findViewById(R.id.toggle);
            Switch switcher = findViewById(R.id.switcher);
    
            LinearLayout test = findViewById(R.id.test);
    
            CompoundButton.OnCheckedChangeListener listener = (button , ischecked) -> {
                if (ischecked){
                    //设置linearlayout垂直布局
                    test.setOrientation(LinearLayout.VERTICAL);
                    toggle.setChecked(true);
                    switcher.setChecked(true);
                }
                else{
                    //设置水平布局
                    test.setOrientation(LinearLayout.HORIZONTAL);
                    toggle.setChecked(false);
                    switcher.setChecked(false);
                }
            };
    
            toggle.setOnCheckedChangeListener(listener);
            switcher.setOnCheckedChangeListener(listener);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    运行效果

    在这里插入图片描述

  • 相关阅读:
    elasticsearch配置密码、docker数据迁移、IP白名单
    分布式合集
    基于SSM SpringBoot vue个人博客网站
    携创教育:2022下半年自考延期省市有哪些?公告是什么?
    【java调取第三方接口,获取数据并保存至数据库】
    ARM Linux DIY(十)LRADC 按键
    Android充电驱动bq24375源码分析
    从低代码的前世今生,看软件开发趋势
    Android Retrofit
    uni-app android picker选择默认月份
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126458845