当前位置: 首页 > news >正文

Android原生实现单选

         六年前写的一个控件,一直没有时间总结,趁年底不怎么忙,整理一下之前写过的组件。供大家一起参考学习。废话不多说,先上图。

一、效果图

实现思路使用的是radioGroup加radiobutton组合方式。原理就是通过修改RadioButton 的background样式实现,radioGroup嵌套radiobutton已经实现单选互斥,故直接监听其选中监听处理数据就好。

dp_0.5:就是0.5dp的意思,自行替换成自己的值。

dp_15:代表15dp
 

二、布局代码

  方案:采用的是RadioGroup嵌套RadioButton的方式。

  <RadioGroupandroid:id="@+id/rg_gender"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/monitor_remd"android:layout_marginLeft="@dimen/dp_24"android:layout_marginTop="@dimen/dp_10"android:orientation="horizontal"android:padding="1dp"><RadioButtonandroid:id="@+id/rb_press_h"android:layout_width="wrap_content"android:layout_height="@dimen/dp_25"android:button="@null"android:checked="true"android:drawableLeft="@drawable/checked_manage"android:drawablePadding="5dp"android:text="高压"android:textColor="@color/white"android:textSize="@dimen/sp_11" /><RadioButtonandroid:id="@+id/rb_press_l"android:layout_width="wrap_content"android:layout_height="@dimen/dp_30"android:layout_marginLeft="10dp"android:button="@null"android:drawableLeft="@drawable/checked_manage"android:drawablePadding="5dp"android:text="低压"android:textColor="@color/white"android:textSize="@dimen/sp_11" /><RadioButtonandroid:id="@+id/rb_press_rate"android:layout_width="wrap_content"android:layout_height="@dimen/dp_30"android:layout_marginLeft="10dp"android:button="@null"android:drawableLeft="@drawable/checked_manage"android:drawablePadding="5dp"android:text="心率"android:textColor="@color/white"android:textSize="@dimen/sp_11" /></RadioGroup>

三、样式代码

  3.1 android:drawableLeft="@drawable/checked_manage"样式

   这种方式使用的是drawableLeft方式,没使用background。

<?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>

3.1.1 press_check样式svg方式实现

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="10dp"android:height="10dp"android:viewportWidth="1024"android:viewportHeight="1024"><pathandroid:pathData="M511.2,63.9c-246.3,0 -447.8,201.5 -447.8,447.8s201.5,447.8 447.8,447.8C757.5,959.6 959,758 959,511.7S757.5,63.9 511.2,63.9zM868.5,662.1c-19.6,46.2 -47.7,87.7 -83.4,123.4s-77.3,63.8 -123.4,83.4c-47.7,20.3 -98.3,30.6 -150.4,30.6 -52.1,0 -102.7,-10.3 -150.4,-30.6 -46.2,-19.6 -87.7,-47.7 -123.4,-83.4s-63.8,-77.3 -83.4,-123.4c-20.3,-47.7 -30.6,-98.3 -30.6,-150.4 0,-52.1 10.3,-102.7 30.6,-150.4 19.6,-46.2 47.7,-87.7 83.4,-123.4s77.3,-63.8 123.4,-83.4c47.7,-20.3 98.3,-30.6 150.4,-30.6 52.1,0 102.7,10.3 150.4,30.6 46.2,19.6 87.7,47.7 123.4,83.4s63.8,77.3 83.4,123.4C888.7,409 899,459.6 899,511.7c0,52.1 -10.3,102.7 -30.5,150.4z"android:fillColor="#2775DE"/><pathandroid:pathData="M511.2,511.7m-170,0a170,170 0,1 0,340 0,170 170,0 1,0 -340,0Z"android:fillColor="#2775DE"/>
</vector>

 3.1.2 nomar_check样式

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="10dp"android:height="10dp"android:viewportWidth="1024"android:viewportHeight="1024"><pathandroid:pathData="M512,182.9c-181.8,0 -329.1,147.4 -329.1,329.1 0,181.8 147.4,329.1 329.1,329.1 181.8,0 329.1,-147.4 329.1,-329.1C841.1,330.2 693.8,182.9 512,182.9zM512,786.3c-151.5,0 -274.3,-122.8 -274.3,-274.3 0,-151.5 122.8,-274.3 274.3,-274.3s274.3,122.8 274.3,274.3C786.3,663.5 663.5,786.3 512,786.3z"android:fillColor="#ffffff"/>
</vector>

3.2 checked_manage样式

<?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>

3.2.1 press_check样式

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="10dp"android:height="10dp"android:viewportWidth="1024"android:viewportHeight="1024"><pathandroid:pathData="M511.2,63.9c-246.3,0 -447.8,201.5 -447.8,447.8s201.5,447.8 447.8,447.8C757.5,959.6 959,758 959,511.7S757.5,63.9 511.2,63.9zM868.5,662.1c-19.6,46.2 -47.7,87.7 -83.4,123.4s-77.3,63.8 -123.4,83.4c-47.7,20.3 -98.3,30.6 -150.4,30.6 -52.1,0 -102.7,-10.3 -150.4,-30.6 -46.2,-19.6 -87.7,-47.7 -123.4,-83.4s-63.8,-77.3 -83.4,-123.4c-20.3,-47.7 -30.6,-98.3 -30.6,-150.4 0,-52.1 10.3,-102.7 30.6,-150.4 19.6,-46.2 47.7,-87.7 83.4,-123.4s77.3,-63.8 123.4,-83.4c47.7,-20.3 98.3,-30.6 150.4,-30.6 52.1,0 102.7,10.3 150.4,30.6 46.2,19.6 87.7,47.7 123.4,83.4s63.8,77.3 83.4,123.4C888.7,409 899,459.6 899,511.7c0,52.1 -10.3,102.7 -30.5,150.4z"android:fillColor="#2775DE"/><pathandroid:pathData="M511.2,511.7m-170,0a170,170 0,1 0,340 0,170 170,0 1,0 -340,0Z"android:fillColor="#2775DE"/>
</vector>

3.2.2 nomar_check样式

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="10dp"android:height="10dp"android:viewportWidth="1024"android:viewportHeight="1024"><pathandroid:pathData="M512,182.9c-181.8,0 -329.1,147.4 -329.1,329.1 0,181.8 147.4,329.1 329.1,329.1 181.8,0 329.1,-147.4 329.1,-329.1C841.1,330.2 693.8,182.9 512,182.9zM512,786.3c-151.5,0 -274.3,-122.8 -274.3,-274.3 0,-151.5 122.8,-274.3 274.3,-274.3s274.3,122.8 274.3,274.3C786.3,663.5 663.5,786.3 512,786.3z"android:fillColor="#ffffff"/>
</vector>

3.3 checked_manage样式

<?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>

四 java代码

     由于使用了databinding双向绑定,故就不演示findviewbyid,如果没有使用databinding,

//高压、低压、心率选择处理
mDataBinding.rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkedId) {switch (checkedId) {case R.id.rb_press_h:Log.d("RadioGroup", "===高压===");break;case R.id.rb_press_l:Log.d("RadioGroup", "===低压===");break;case R.id.rb_press_rate:Log.d("RadioGroup", "===心率===");break;}}
});

五 给予新手的寄语

     对于新手来讲,开发总会遇到各种各样的问题,逐个解决就好,注意沟通协调。

http://www.lryc.cn/news/268680.html

相关文章:

  • 为什么需要对数值类型的特征做归一化?
  • ARM 点灯
  • CamSim相机模拟器:极大加速图像处理开发与验证过程
  • Google Ad帐号被封?代理IP和电子邮件可能是原因
  • EfficientNet
  • 百度每天20%新增代码由AI生成,Comate SaaS服务8000家客户 采纳率超40%
  • 产品管理-学习笔记-版本的划分
  • 编程笔记 html5cssjs 004 我的第一个页面
  • 为实体服务器配置Ubuntu
  • 单例模式的双重检查锁定是什么?
  • hyper-v ubuntu 3节点 k8s集群搭建
  • postman进阶使用
  • errors包返回堆栈信息的性能测试
  • 力扣热题100道-哈希篇
  • YOLOv7+Pose姿态估计+tensort部署加速
  • gitee+picgo+typora图床搭建
  • Flink项目实战篇 基于Flink的城市交通监控平台(上)
  • thinkcmf 文件包含 x1.6.0-x2.2.3 已亲自复现
  • 本地部署 text-generation-webui
  • C语言实验1:C程序的运行环境和运行C程序的方法
  • 「微服务」微服务架构中的数据一致性
  • ARCGIS PRO SDK 要素空间关系
  • Python面向对象高级与Python的异常、模块以及包管理
  • Python 爬取 哔站视频弹幕 并实现词云图可视化
  • BP神经网络详细原理,BP神经网络训练界面详解,基于BP神经网络的公司财务风险分类
  • C++ DAY1 作业
  • 「微服务模式」七种微服务反模式
  • 运动耳机哪款性价比最高、性价比最高的运动耳机推荐
  • FreeRTOS软件定时器
  • 【Java集合类不安全示例】