本文只为初级的Android新手而写,多掌握一份简单实用的技能,快速get吧,有问题就留言

2022:蓦然回首,已入行多年,人生的第二个迷茫阶段

  • 初级 - 使用方式
    • RadioGroup + RadioButton
    • CheckBox
    • Demo示例
  • CheckBox 自定义选中、取消样式
    • selector 方式
    • style 方式
    • 修改选中状态的颜色
    • CheckBox 点击无效
    • 去除CheckBox默认边距

Let,s gogogo ~

初级 - 使用方式

RadioGroup + RadioButton

提醒:使用RadioGroup+RadioButton的时候,如果要实现向我们效果图中一样的效果,需以下操作

  • 首先原有的Button需要设置为null,也就是Xml中的android:button="@null"
  • 同时自己定义一个样式Drawable下创建一个selector,如(图片资源自己找哈)
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/press_check" android:state_checked="true"></item><item android:drawable="@drawable/nomar_check"></item>
</selector>

xml

    <RadioGroupandroid:padding="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rg_gender"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:checked="true"android:button="@null"android:drawableLeft="@drawable/gender_bg"android:drawablePadding="5dp"/><RadioButtonandroid:id="@+id/rb_gril"android:layout_width="wrap_content"android:text="女"android:button="@null"android:drawablePadding="5dp"android:drawableLeft="@drawable/gender_bg"android:layout_marginLeft="10dp"android:layout_height="wrap_content" /></RadioGroup>

RadioGroup+RadioButton 使用方式

        //多个选项,仅支持单选(常见在主页UI架构)RadioGroup mGender = (RadioGroup) findViewById(R.id.rg_gender);RadioButton mGril = (RadioButton) findViewById(R.id.rb_gril);RadioButton mBoy = (RadioButton) findViewById(R.id.rb_boy);//初始化性别,并没有把下面监听的内容直接set进来,所以需要点击后生效,如果想操作可以在这里直接setTextmBoy.setChecked(true);//单选监听mGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId==mBoy.getId()){mGenderContent.setText("您老人家竟然是个男的");}else{mGenderContent.setText("您老人家竟然是个女的???");}}});
CheckBox

CheckBox 单独监听

        //复选框 - 单独监听mState.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();}}});

初级写法

        //多个复选框CheckBox mState1 = (CheckBox) findViewById(R.id.cb_state1);CheckBox mState2 = (CheckBox) findViewById(R.id.cb_state2);CheckBox mState3 = (CheckBox) findViewById(R.id.cb_state3);//多个复选框   因为是初级操作,并没有全部放在list下进行数据集体显示 mState1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState1.getText().toString().trim());Toast.makeText(MainActivity.this,"北京被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState2.getText().toString().trim());Toast.makeText(MainActivity.this,"上海被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState3.getText().toString().trim());Toast.makeText(MainActivity.this,"广州被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});}

常见写法

         //多个复选框CheckBox mState1 = (CheckBox) findViewById(R.id.cb_state1);CheckBox mState2 = (CheckBox) findViewById(R.id.cb_state2);CheckBox mState3 = (CheckBox) findViewById(R.id.cb_state3);mState1.setOnCheckedChangeListener(this);mState2.setOnCheckedChangeListener(this);mState3.setOnCheckedChangeListener(this);@Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) {if(compoundButton.isChecked())Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();}
Demo示例

MainActivity

package com.example.dow.statelistener;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private CheckBox mState1;private CheckBox mState2;private CheckBox mState3;private CheckBox mState;private RadioButton mBoy;private RadioButton mGril;private RadioGroup mGender;private TextView mGenderContent;private TextView mContent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//内容展示区mGenderContent = (TextView) findViewById(R.id.tv_gender);//单选框mGender = (RadioGroup) findViewById(R.id.rg_gender);mGril = (RadioButton) findViewById(R.id.rb_gril);mBoy = (RadioButton) findViewById(R.id.rb_boy);//复选框mState = (CheckBox) findViewById(R.id.cb_state);//多个复选框mState1 = (CheckBox) findViewById(R.id.cb_state1);mState2 = (CheckBox) findViewById(R.id.cb_state2);mState3 = (CheckBox) findViewById(R.id.cb_state3);mContent = (TextView) findViewById(R.id.tv_content);//初始化性别,并没有把下面监听的内容直接set进来,所以需要点击后生效,如果想操作可以在这里直接setTextmBoy.setChecked(true);//单选监听mGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId==mBoy.getId()){mGenderContent.setText("您老人家竟然是个男的");}else{mGenderContent.setText("您老人家竟然是个女的???");}}});//复选框 - 单独监听mState.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();}}});//多个复选框   因为是初级操作,并没有全部放在list下进行数据集体显示 mState1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState1.getText().toString().trim());Toast.makeText(MainActivity.this,"北京被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState2.getText().toString().trim());Toast.makeText(MainActivity.this,"上海被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState3.getText().toString().trim());Toast.makeText(MainActivity.this,"广州被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});}
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.dow.statelistener.MainActivity"><TextViewandroid:layout_width="match_parent"android:id="@+id/tv_gender"android:padding="5dp"android:layout_height="wrap_content"android:text="性别展示区:" /><RadioGroupandroid:padding="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rg_gender"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:checked="true"android:button="@null"android:drawableLeft="@drawable/gender_bg"android:drawablePadding="5dp"/><RadioButtonandroid:id="@+id/rb_gril"android:layout_width="wrap_content"android:text="女"android:button="@null"android:drawablePadding="5dp"android:drawableLeft="@drawable/gender_bg"android:layout_marginLeft="10dp"android:layout_height="wrap_content" /></RadioGroup><LinearLayoutandroid:layout_marginTop="30dp"android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你是不是人妖?"/><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state"android:layout_height="wrap_content" /></LinearLayout><TextViewandroid:layout_width="match_parent"android:id="@+id/tv_content"android:padding="5dp"android:layout_height="wrap_content"android:text="展示区:" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state1"android:text="北京"android:layout_height="wrap_content" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state2"android:text="上海"android:layout_height="wrap_content" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state3"android:text="广州"android:layout_height="wrap_content" />
</LinearLayout>

CheckBox 自定义选中、取消样式

这些都是在2022年补入的一些东西,虽然初级,但是还是很常用的... 真实不知不觉这么多年了

selector 方式

drawable 创建 selector 选择器 (我在项目中创建了selector 选择器 - select_login_state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_enabled="true"android:state_checked="false"android:drawable="@drawable/ic_login_unselected"/><itemandroid:state_enabled="true"android:state_checked="true"android:drawable="@drawable/ic_login_selected"/>
</selector>

引用方式 android:button="@drawable/select 选择器"

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:layout_height="wrap_content" />
style 方式

创建 CheckBoxstyle

CheckBox 无边距(常用)

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="android:button">@drawable/select_login_state</item></style>

或者 CheckBox 有默认边距

  <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="android:button">@drawable/select_login_state</item></style>

xml引用方式 style="@style/checkboxStyle"

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"style="@style/checkboxStyle"android:layout_height="wrap_content" />

修改选中状态的颜色

场景:按理我设置选择器后,选中后为黑色图标,但是老师显示蓝色图标(theme原因)

创建 CheckBoxstyle

CheckBox 无边距(常用 - UI美观,但有bug)

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item><item name="android:button">@drawable/select_login_state</item></style>

或者 CheckBox 有默认边距

    <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item></style>

xml引用方式 android:theme="@style/checkboxStyle""android:theme是修改选中颜色的关键属性

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:theme="@style/checkboxStyle"android:button="@drawable/select_login_state"android:layout_height="wrap_content" />

CheckBox 点击无效

首先要看一下发生这个问题的场景是否设置了theme或引用了一些自定义的style?

别人的解决方式:有人说在xml中设置:android:clickable="true" ,但是对我无效!

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:clickable="true"android:theme="@style/checkboxStyle"android:layout_height="wrap_content" />

我的解决方式

提示:首先我设置checkBox的方式,都是通过上面的操作实现的

  <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:layout_height="wrap_content" />

因为问题主要在theme用到的style,我都说一下

theme style = parent="Theme.AppCompat.Light",导致CheckBox点击无效

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item><item name="android:button">@drawable/select_login_state</item></style>

将theme style 改为 parent="@android:style/Widget.CompoundButton.CheckBox",可正常点击,但会出现视图间距、边距(想解决这点,继续往下看)

    <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item></style>

去除CheckBox默认边距

关于默认边距问题,主要使用以下俩个属性

 android:minHeight="0dp"android:minWidth="0dp"

例如(不过这样设置过后,有可能点击又无效了

<CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:minWidth="@dimen/dp_0"android:minHeight="@dimen/dp_0"android:layout_height="wrap_content" />

仅设置android:minWidth="0dp" 属性,可解决CheckBox无法点击的问题

<CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:minWidth="@dimen/dp_0"android:layout_height="wrap_content" />

Android入门之路 - RadioGroup、RadioButton、CheckBox(单复选框)使用进阶相关推荐

  1. php中得到复选框的数据的代码,表单复选框向PHP传输数据的代码

    表单复选框向PHP传输数据的代码 表单复选框就是checkbox 1.checkbox的应用 复制代码 代码如下: 2.由于我传输的是在php循环中产生的数组,因此value也要设成变量: for($ ...

  2. JS对象迭代、事件处理器、表单控件绑定、表单复选框、表单单选按钮

    JS对象迭代 知识点 v-for v-for 循环JS对象,把对象内容循环显示到页面上. <div id="myApp"><h1>JS对象迭代</h1 ...

  3. Layui表单复选框验证

    Layui表单复选框验证 近日由于项目原因使用layui框架进行开发,在做表单验证的时候苦于复选框验证问题找不到答案,于是作为小白的我换了一种思路,不采用官方提供的form-verify,而是采用在提 ...

  4. HTML中 单复选框的用法

    今天看了看单复选框的用法,感觉很是好玩儿 ,所以赶快来把学会的分享下! 首先,在使用表单设计调查问卷时,为了减少用户的操作,使用选择框是一个不错的选择. HTML中有两种选择框,分别为单选框和复选框. ...

  5. python图形编程复选按钮和单选按钮详细说明_Python_tkinter_单选框(Radiobutton)与复选框(Checkbutton)...

    单选框(Radiobutton)与复选框(Checkbutton) thinter.Radiobutton(用于存放的父组件,属性参数...) thinter.Checkbutton(用于存放的父组件 ...

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

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

  7. 纯CSS美化单复选框(checkbox、radio)

    最重要的一点,隐藏选择框本身.不多说了,上代码: <!doctype html> <html> <head> <meta charset="utf- ...

  8. html构建复选框标签,什么标签用于在表单中构建复选框_HTML表单复选框INPUT标签...

    HTML表单复选框标签 在HTML的表单控件中,复选框也是经常使用的控件,它可以让用户选择打勾或不打勾.它使用的也是标签. 一.定义 标签用于表示文本框.密码框.单选框.复选框.文件上传框.普通按钮. ...

  9. ICheck表单复选框、单选框控件美化插件

    作用: 渲染并美化当前页面的复选框或单选框 响应复选框或单选框的点击事件 特点: 在不同浏览器(包括ie6+)和设备上都有相同的表现 - 包括 桌面和移动设备 支持触摸设备 - iOS.Android ...

最新文章

  1. 免费机器学习课程爆红:从概率与统计到全栈深度学习,英伟达工程师小姐姐整理...
  2. 操作系统实验报告17:请求页面置换算法
  3. GrepWin:Win7下的文本替换工具
  4. java 下载图片 弹出框_javaweb多图片打包下载,弹出提示框问题_html/css_WEB-ITnose
  5. 如何使用Movavi Video Editor编辑婚礼影片?
  6. 用网速作为手机信号强度
  7. Angular Mock Data
  8. java opencv 模板匹配算法_OpenCV模板匹配算法详解
  9. Java入门需要了解(面向对象之接口-十四)
  10. web前端一张页面多少钱?
  11. 深度学习之ISAR超分辨率成像
  12. 教你三分钟上手阿里云OOS上传操作
  13. weka分类器怎么设置样本类别_NeurIPS 2020 | 港中文MMLab自步对比学习: 充分挖掘无监督学习样本...
  14. BIOS中VT虚拟技术已经开启,但任务管理器中仍显示虚拟化已禁用
  15. 仿酒仙网品牌活动动画效果 (鼠标移上 图片平移)
  16. 福大软工 · 第八次作业(课堂实战)- 项目UML设计(团队)
  17. Stackoverflow使用
  18. 浙里办APP的系统架构分析
  19. stm32-esp8266驱动程序
  20. Mentor-dft 学习笔记 day7-drc规则环路可测试性方法

热门文章

  1. python大型游戏脚本_论做游戏脚本,Python输过谁?
  2. 最详细的idea创建webservice教程
  3. Java:eclipse下开发webservice教程
  4. 小米手机升级系统后无法真机调试
  5. 免费数据库(SQLite、Berkeley DB、PostgreSQL、MySQL、Firebird、mSQL、MSDE、DB2 Ex
  6. 矮胖企鹅NFT创下历史新高,反超CloneX和Doodles等蓝筹
  7. 在excel表格中统计及格人数的详细教程步骤
  8. python prettytable输出对齐_Python 使用 prettytable 库打印表格美化输出功能
  9. 旋转任意角度 如何让div旋转一定的角度 (转)
  10. CCF 201612-2工资计算