热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidRadioGroup&CheckBox基操与进阶

Android–RadioGroup&CheckBox基操与进阶先上效果图(全部demo):一.认识RadioButton和RadiaoGroup:RadioButton:一个自

Android – RadioGroup & CheckBox 基操与进阶

先上效果图(全部demo):
在这里插入图片描述


一. 认识RadioButton 和 RadiaoGroup:



  1. RadioButton:一个自带图标样式的Button, 具有两种状态,选中或未选中;(单个RadioButton在选中后,无法通过点击变为未选中)



  2. RadioGroup:当需要多个RadioButton, 但每次仅需选中其中一个时,使用其管理内部的RadioButton。

    基本使用:使用默认图标,自定义选中和未选中状态下字体颜色

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="@dimen/padding_15"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="default"android:textSize="@dimen/text_size_20"android:padding="@dimen/padding_5"/><RadioGroupandroid:id="@+id/rGroup1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButtonandroid:id="@+id/rButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Male"android:textColor="@drawable/selector_text"android:checked="true" /><RadioButtonandroid:id="@+id/rButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Female"android:textColor="@drawable/selector_text"/>RadioGroup>
    LinearLayout>

    selector_txt.xml


    <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:color="@color/colorPrimary"/><item android:state_checked="false" android:color="@color/colorBlack"/>
    selector>


在这里插入图片描述


二. 了解CheckBox:



  1. CheckBox: 具有两种状态,选中或未选中;具有独立性,多个可同时选中,适用于多种效果叠加;与RadioButton属于同一级别。

  2. 与RadioButton的区别:

    1. 单个CheckBox在选中后,可以通过再次点击变为未选中状态;

    2. 一组CheckBox,可同时选中多个;



基本使用:使用默认图标,自定义选中和未选中状态下字体颜色

<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><CheckBoxandroid:id="@+id/checkbox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="貂蝉"android:textColor="@drawable/selector_text"/><CheckBoxandroid:id="@+id/checkbox2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="孙尚香"android:textColor="@drawable/selector_text"/><CheckBoxandroid:id="@+id/checkbox3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="公孙离"android:textColor="@drawable/selector_text"/>LinearLayout>

在这里插入图片描述


三. 进阶:自定义选中样式



  1. 样式一:隐藏图标,随当前状态改变文字背后的背景
    在这里插入图片描述

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="@dimen/padding_15"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="self icon"android:textSize="@dimen/text_size_20"android:padding="@dimen/padding_5"/><RadioGroupandroid:id="@+id/rGroup2"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rButton3"android:layout_width="50dp"android:layout_height="wrap_content"android:layout_margin="@dimen/margin_10"android:background="@drawable/selector_check_bg"android:button="@null"android:checked="true"android:gravity="center"android:text="Male" /><RadioButtonandroid:id="@+id/rButton4"android:layout_width="50dp"android:layout_height="wrap_content"android:layout_margin="@dimen/margin_10"android:gravity="center"android:text="Female"android:button="@null"android:background="@drawable/selector_check_bg" />RadioGroup>
    LinearLayout>

    selector_check_bg.xml


    <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true"><shape android:shape="rectangle"><stroke android:color="@color/colorPrimary"android:width="1dp"/><solid android:color="@color/colorPrimary"/><corners android:radius="5dp"/>shape>item><item android:state_checked="false"><shape android:shape="rectangle"><stroke android:color="@color/colorGery"android:width="1dp"/><solid android:color="@color/colorGery"/><corners android:radius="5dp"/>shape>item>
    selector>


  2. 样式二:使用自己的状态图标
    在这里插入图片描述

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="@dimen/padding_15"><RadioGroupandroid:id="@+id/rGroup3"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rButton5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="@dimen/margin_10"android:text="Male"android:textColor="@drawable/selector_text"android:gravity="center"android:button="@drawable/radio_button_style"android:checked="true"/><RadioButtonandroid:id="@+id/rButton6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="@dimen/margin_10"android:gravity="center"android:text="Female"android:textColor="@drawable/selector_text"android:button="@drawable/radio_button_style"/>RadioGroup>
    LinearLayout>

    radio_button_style.xml


    <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:state_enabled="true" android:state_pressed="true"android:drawable="@drawable/icon_locate_blue"/><item android:state_checked="false" android:state_enabled="true" android:state_pressed="true"android:drawable="@drawable/icon_locate_blue"/><item android:state_checked="true" android:state_enabled="true" android:drawable="@drawable/icon_locate_blue"/><item android:state_checked="false" android:state_enabled="true" android:drawable="@drawable/icon_locate_gery"/><item android:state_checked="true" android:state_enabled="false" android:drawable="@drawable/icon_locate_blue"/><item android:state_checked="false" android:state_enabled="false" android:drawable="@drawable/icon_locate_gery"/>
    selector>

    CheckBox(与RadioButton可用同一配置文件):
    在这里插入图片描述

    <LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:padding="@dimen/padding_15"><CheckBoxandroid:id="@+id/checkbox4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="@dimen/padding_5"android:text="阿离"android:textColor="@drawable/selector_text"android:button="@drawable/radio_button_style"/><CheckBoxandroid:id="@+id/checkbox5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="@dimen/padding_5"android:text="苏苏"android:textColor="@drawable/selector_text"android:button="@drawable/radio_button_style"/>
    LinearLayout>``



推荐阅读
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文介绍了一个Magento模块,其主要功能是实现前台用户利用表单给管理员发送邮件。通过阅读该模块的代码,可以了解到一些有关Magento的细节,例如如何获取系统标签id、如何使用Magento默认的提示信息以及如何使用smtp服务等。文章还提到了安装SMTP Pro插件的方法,并给出了前台页面的代码示例。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 本文介绍了如何使用PHP代码将表格导出为UTF8格式的Excel文件。首先,需要连接到数据库并获取表格的列名。然后,设置文件名和文件指针,并将内容写入文件。最后,设置响应头部,将文件作为附件下载。 ... [详细]
author-avatar
yico承诺
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有