饭后Android 第二餐-复选框CheckBox+开关按钮Switch+单选按钮RadioButton

  • 1.CompoundButton概述
  • 1.复选框CheckBox
    • 1.UI布局
    • 2按钮监听
  • 2.开关按钮Switch
    • 1.UI布局
    • 2.按钮监听
  • 3.单选按钮RadioButton
    • 1.UI布局
    • 2.按钮监听

饭后Android 第一餐-导航视图NavigationView+抽屉布局DrawerLayout+工具栏Toolbar

1.CompoundButton概述

在Android中,CompoundButton类是抽象类的复合按钮,因为是抽象类,所以不能直接使用,实际开发中用的是
CompoundButton类的几个派生类,主要有复选框,开关按钮Switch和单选按钮RadioButton,这些派生类都可使用CompoundButton的属性和方法

CompoundButton 布局文件属性

  • android:checked : 指定按钮的勾选状态,ture表示勾选,flase表示未勾选,默认为未勾选
  • android:button:指定左侧勾选图标的图形,如果不指定就使用系统默认的图标

CompoundButton 在逻辑代码中可以使用下列四种方法进行设置

  • setChecked:设置按钮的勾选状态
  • setButtonDrawable:设置左侧勾选图标的图形
  • setOnCheckedChangeListener:设置勾选状态的监听器
  • isChecked:判断按钮是否勾选

1.复选框CheckBox

复选框CheckBox是CompoundButton 一个最简单的实现,点击复选框勾选,再次点击取消勾选,CheckBox通过
setOnCheckedChangeListener方法实现监听器,对应的监听器要实现接口CompoundButton .setOnCheckedChangeListener.

1.UI布局

把四个复选框嵌套到了一个约束布局里面,然后用了一个自定义按钮图形

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请选择你的课程"android:textColor="#000000"android:textSize="24dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />app:layout_constraintTop_toBottomOf="@id/check_3" /><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/con"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@id/textView"><CheckBoxandroid:id="@+id/check_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Android"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="@id/con" /><CheckBoxandroid:id="@+id/check_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="JAVA"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/check_1" /><CheckBoxandroid:id="@+id/check_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C语言"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/check_2" /><CheckBoxandroid:id="@+id/check_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="算法"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/check_3" /></androidx.constraintlayout.widget.ConstraintLayout><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提交"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/check_5" /><CheckBoxandroid:id="@+id/check_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:text="确认"android:button="@drawable/button_style"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/con" /></androidx.constraintlayout.widget.ConstraintLayout>

按钮样式代码 ---------button_style

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/a2"/><item android:drawable="@drawable/a1"/></selector>

2按钮监听


public class MainActivity extends AppCompatActivity {ConstraintLayout mConstraintLayout;Button mButton;CheckBox mCheckBox;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mButton=findViewById(R.id.button);mConstraintLayout=findViewById(R.id.con);mCheckBox=findViewById(R.id.check_5);mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//设置监听器@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){Toast.makeText(MainActivity.this, "确认成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"请确认选择", Toast.LENGTH_SHORT).show();}}});mButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mCheckBox.isChecked()) {StringBuffer stringBuffer=new StringBuffer();//StringBuffer本质上是一个字符数组int Num = mConstraintLayout.getChildCount();  //拿到所有的子类长度for (int i = 0; i < Num; i++) {//根据i 拿到每一个CheckBoxCheckBox checkBox= (CheckBox) mConstraintLayout.getChildAt(i);if(checkBox.isChecked()){ //判断CheckBox是否被选中//把被选中的文字添加到StringBuffer中stringBuffer.append(checkBox.getText().toString()+" ");}}Toast.makeText(MainActivity.this, "成功选择课程"+stringBuffer.toString(), Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this,"请确认选择",Toast.LENGTH_SHORT).show();}}});}

补充一:
设置 确认 复选框的监听器,通过实现接口CompoundButton.OnCheckedChangeListener来对复选框进行监听,再通过判断是否被选中来“吐丝”

补充二:
在 提交 按钮的点击事件里,首先先判断 确认 按钮是否被选中,如果没被选中,就会打印请确认选择,如果被选中,就会新建了一个StringBuffer字符数组,用来存复选框中的文字内容,然后再拿到之前约束布局里嵌套的子类数量,通过for循环来判断子类中的复选框是否被选中,并且将选中的目标内容存进字符数组,在for循环结束后通过toast 打印在屏幕上

运行

2.开关按钮Switch

Switch是开关按钮,Android从4.0版本开始支持该控件,其实Switch是一个高级版本的CheckBox,在选中与取消选中时可展现的界面元素比CheckedBox丰富,

1.UI布局

方法一:通过xml来设置
Switch新添加的xml属性

xml中的属性 说明
textOn 设置右侧开启时的文本
textOff 设置左侧关闭时的文本
switchPadding 设置左右两个开关按钮之间的距离
thumbTextPadding 设置文本左右两边的距离,如果设置了该属性,switchPadding属性就会失效
thumb 设置开关轨道的背景
track 设置开关标识的图标

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><Switchandroid:id="@+id/switc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:thumbTextPadding="10dp"android:showText="true"android:textOff="@string/textOff"android:textOn="@string/textOn"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

方法二:通过java代码来设置

Switch类的设置方法 说明
setTextOn 设置右侧开启时的文本
setTextOff 设置左侧关闭时的文本
setSwitchPadding 设置左右两个开关按钮之间的距离
setThumbTextPadding 设置文本左右两边的距离,如果设置了该属性,switchPadding属性就会失效
thumb 设置开关轨道的背景
track 设置开关标识的图标

java代码


public class MainActivity extends AppCompatActivity {Switch mSwitch;@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwitch=findViewById(R.id.switc);mSwitch.setShowText(true);mSwitch.setTextOn(" 打开" );mSwitch.setTextOff(" 关闭" );mSwitch.setThumbTextPadding(15);}
}

xml代码(可以看到xml没有设置属性的代码)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"><Switchandroid:id="@+id/switc"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

2.按钮监听

public class MainActivity extends AppCompatActivity {Switch mSwitch;TextView mTextView;@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwitch=findViewById(R.id.switc);mTextView=findViewById(R.id.text);mTextView.setText("开关状态为关");mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (mSwitch.isChecked()) {mTextView.setText("开关状态为开");}else {mTextView.setText("开关状态为关");}}});}
}

运行

3.单选按钮RadioButton

单选按钮要在一组中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是RadioGroup,RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下,RadioGroup有orientation属性可指定下级控件的排列方向,该属性为horizontal时,单选按钮在水平方向排列,该属性为vertical时,单选按钮在垂直方向排列,RadioGroup下面除了RadioButton,还可以挂载其他子控件(如TextView,ImageView等)这样看来,RadioGroup就是一个特殊的线性布局,只不过多了管理单选按钮的功能

1.UI布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><RadioGroupandroid:id="@+id/radiogroup"android:layout_width="match_parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/text"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textColor="#000000"android:text="请选择一项你喜欢的体育项目"/><RadioButtonandroid:id="@+id/radiobutton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="篮球"/><RadioButtonandroid:id="@+id/radiobutton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="游泳"/><RadioButtonandroid:id="@+id/radiobutton3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跑步"/><RadioButtonandroid:id="@+id/radiobutton4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="羽毛球"/></RadioGroup></androidx.constraintlayout.widget.ConstraintLayout>

2.按钮监听

RadioGroup 代码中常用的3个方法

  • check:选中指定资源编号的单选按钮
  • getCheckRadioButton:获取选中状态单选按钮的资源编号
  • setOnCheckedChangeListener:设置单选按钮勾选变化的监听器

RadioButton默认未选中,点击后显示选中,但是再次点击不会取消选中,只有点击同组的其他单选按钮时,原来的单选按钮才会取消选中,另外,单选按钮的选中事件一般不由RadioButton处理,而是由RadioGroup响应,选中事件在实现时,首先要写一个单选监听器实现接口RadioGroup。OnCheckedChangeListenter,然后调用RadioGroup对象的 setOnCheckChangeListener方法注册该监听器。


public class MainActivity extends AppCompatActivity {RadioGroup mRadioGroup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mRadioGroup=findViewById(R.id.radiogroup);mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {//在用户点击组内的单选按钮时触发RadioButton radioButton=findViewById(checkedId);String string=radioButton.getText().toString();Toast.makeText(MainActivity.this,"成功选择"+string,Toast.LENGTH_SHORT).show();}});}
}

运行

ok 以上就是本餐的饭后甜点啦,希望小伙伴们可以好好品尝。谢谢您的阅读

想不到有朝一日我在生日这一天还在学习,我觉得这样真的挺好的,今天是20岁的第一天,我想我的人生就从今天开始就要迈向更高的位置吧,为了让自己和家人过上更好的生活,也为了自己的理想,加油。20岁的ROSE J,火力全开
博主为了可以学到更多的Android知识,创建了一个安卓知识交流群,欢迎大佬入群,当然也欢迎和我一样的安卓小白,我们可以一起交流,最重要的是快乐水群,记得定个小目标,冲击bat

饭后Android 第二餐-复选框CheckBox+开关按钮Switch+单选按钮RadioButton相关推荐

  1. Android Studio App开发入门之选择按钮的讲解及使用(包括复选框,开关按钮,单选按钮,附源码)

    运行有问题或需要图片资源请点赞关注收藏后评论区留言~~~ 在学习复选框之前,先了解一下CompoundButton,在Android体系中,CompoundButton类是抽象的复合按钮,因为是抽象类 ...

  2. Android学习之复选框checkbox自定义样式以及调整图片大小

    1.自定义样式: 下载复选框样式图:https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2 图片重命名为英文(否则找不到)并 ...

  3. 【Android】-- 按钮(复选框CheckBox、开关按钮Switch、单选按钮RadioButton)

    CompoundButton在XML文件中主要使用下面两个属性. checked:指定按钮的勾选状态,true表示勾选,false则表示未勾选,默认为未勾选. button:指定左侧勾选图标的图形资源 ...

  4. Android之单复选框及Spinner实现二级联动

    一.基础学习 1.图形学真的很神奇啊....查了些资料做出了3D云标签,哈哈...其实直接拿来用的,我们要效仿鲁迅先生的拿来主义,嘿嘿~~3D标签云就是做一个球面,然后再球面上取均匀分布的点,把点坐标 ...

  5. MFC复选框CheckBox使用 ++

    MFC中复选框checkbox控件,至少有四种方法对其进行操作, 第一种是利用Cbutton成员函数GetCheck和SetCheck, 第二种是利用CWnd成员函数IsDlgButtonChecke ...

  6. MFC复选框CheckBox使用

    MFC中复选框checkbox控件,至少有三种方法对其进行操作,他们是利用Cbutton成员函数GetCheck和SetCheck,第二种是利用CWnd成员函数IsDlgButtonChecked,最 ...

  7. 【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  8. jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等

    jQuery操作复选框checkbox技巧总结 --- 设置选中.取消选中.获取被选中的值.判断是否选中等 一.checked属性定义 先了解下input标签的checked属性: 1.HTML &l ...

  9. 为tableview添加带控件的单元格如复选框checkbox与combbox单元格

    我们常常会有这样的需求,为QTableView增加复选框checkbox和选择下拉框combbox,毕竟依靠键盘输入不是很好约束其规范性.下面我们逐个来介绍.完成之后的效果如下: 一.准备TableV ...

  10. 使用CSS3美化复选框checkbox

    我们知道HTML默认的复选框样式十分简陋,而以图片代替复选框的美化方式会给页面表单的处理带来麻烦,那么本文将结合实例带您一起了解一下使用CSS3将复选框checkbox进行样式美化,并且带上超酷的滑动 ...

最新文章

  1. python批量jpg转png(顺序排列1.2.3……)、修改文件夹尺寸
  2. 干货 | 吴恩达亲自为这份深度学习专项课程精炼图笔记点了赞!(附下载)
  3. 反弹和补遗:再论Bjarne Stroustrup的基于对象的含义
  4. 青云服务器安全设置器2.3最新版版提供下载
  5. github 2FA里的recovery code,一定要好好保存
  6. 关于JAVA_HOME, CLASSPATH和PATH的设置
  7. matlab使用tic 和 toc记录程序执行时间
  8. Python实现 QQ 半自动发送情话,我追到了女神
  9. tensorflow随笔——Yolo v1
  10. 软件测试必问必背面试题
  11. Delphi7--多分支Case语句
  12. Stm32 DHT11
  13. camille mumu 模拟器 frida 踩坑记录
  14. 《APUE.3E》用gdb调试ftw函数(图4-22)
  15. Unity做360度全景预览
  16. linux中dd命令详解,Linux dd命令详解
  17. [文摘20070509]管仲
  18. python opencv 轮廓检测_opencv之轮廓检测与处理
  19. 网格电流例题(cubic resistor|立方网格电流)
  20. linux指纹登录实现原理,指纹识别技术原理与基于Linux系统的指纹识别门禁系统设计...

热门文章

  1. ScrollView嵌套Tablaout与ViewPager联动(标签)(ScrollView与ViewPager冲突)
  2. android Glide 去掉绿色背景(图片变绿解决方法)
  3. 【物联网】Arduino Uno开发板连接阿里云实现云端远程控制LED灯开关
  4. 腾达路由器登录远端服务器无响应,腾达路由器 192.168.0.1(tendawifi.com)打不开,怎么办?...
  5. kotlin发音!2021年Android面试心得,安卓系列学习进阶视频
  6. 1、当鼠标移动到目标上的时候,自动显示一个提示框。
  7. ubuntu 选择独立显卡或则intelcpu内集成显卡
  8. Unable to boot device in current state: Creating
  9. [Python高效编程] - 统计元素出现频度
  10. Pytorch教程[03]transforms