目录

  • RadioButton(单选框)
    • 1.获取选中的值
    • 2.运用
    • 3.效果展示
  • CheckBox(复选框)
    • 1.布局
      • 自定义点击效果
      • 修改文字与选择框的距离
    • 2.MainActivity.java文件
    • 3.效果展示

RadioButton(单选框)

RadioButton(单选按钮),就是之能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现单选功能。我们可以为外层RadioButton设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平。
RadioButton是继承LinearLayout

1.获取选中的值

第一种:是为RadioButton设置一个事件监听器:setOnCheckedChangeListener()

ps:一定要切记,要为每个RadioButton添加一个id,不然单选功能不会生效!

RadioGroup rbtn=(RadioGroup) findViewById(R.id.group);//第一种获得单选按钮值的方法为radioGroup设置一个监听器:setOnCheckedChangeListener()rbtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton radioButton=(RadioButton) findViewById(checkedId);Toast.makeText(getApplicationContext(),"你选了:"+radioButton.getText(),Toast.LENGTH_SHORT).show();}});

第二种:通过点击其他按钮获取选中的单选按钮的值

Button btn=findViewById(R.id.btn);
RadioGroup rbtn=(RadioGroup) findViewById(R.id.group);
//第二种为RadioGroup设置一个监听器:setOnClickListener()btn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {//getChildCount()获得按钮组中的单选按钮的数目for(int i=0;i<rbtn.getChildCount();i++){//getChildAt()根据索引值获取我们的单选按钮RadioButton rd=(RadioButton) rbtn.getChildAt(i);//isChecked()判断按钮是否被选中if(rd.isChecked()){Toast.makeText(getApplicationContext(),"你选择的是:"+rd.getText(),Toast.LENGTH_SHORT).show();}}}});

2.运用

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".MainActivity"><RadioGroupandroid:id="@+id/group"android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:checked="true"android:textSize="30sp"/><RadioButtonandroid:id="@+id/woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:textSize="30sp"/></RadioGroup><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="提交"android:textSize="30dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:backgroundTint="#8BC34A"/>
</LinearLayout>

2.MainActivity.java文件

package com.example.day_17;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn=findViewById(R.id.btn);//提交按钮RadioGroup rbtn=(RadioGroup) findViewById(R.id.group);//第一种获得单选按钮值的方法为radioGroup设置一个监听器:setOnCheckedChangeListener()rbtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton radioButton=(RadioButton) findViewById(checkedId);//checkedId获取被选中的单选框的idToast.makeText(getApplicationContext(),"你选了:"+radioButton.getText(),Toast.LENGTH_SHORT).show();}});//第二种为RadioGroup设置一个监听器:setOnClickListener()btn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {//getChildCount()获得按钮组中的单选按钮的数目for(int i=0;i<rbtn.getChildCount();i++){//getChildAt()根据索引值获取我们的单选按钮RadioButton rd=(RadioButton) rbtn.getChildAt(i);//isChecked()判断按钮是否被选中if(rd.isChecked()){Toast.makeText(getApplicationContext(),"你选择的是:"+rd.getText(),Toast.LENGTH_SHORT).show();}}}});}
}

3.效果展示

CheckBox(复选框)

既可以同时选中多个选项。

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".ChexBox_MainActivity"><CheckBoxandroid:id="@+id/first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Java"android:textSize="30sp"/><CheckBoxandroid:id="@+id/second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Android"android:background="@null"android:paddingLeft="40dp"android:textSize="30sp"/><CheckBoxandroid:id="@+id/three"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/check_btn"android:text="C++"android:textSize="30sp" /><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="提交"android:textSize="30sp"android:backgroundTint="#8BC34A"/></LinearLayout>

自定义点击效果

1.创建check_btn.xml文件


2.编辑check_btn.xml文件

ps:drawable在放入图片的时候,需要将图片保存为xxxhdpi,这样图片展示出来的就不会太大。具体为什么要保存为xxxhdpi,大家可以试试类型的,会发现图片显示出来的很大。
可以理解一下android drawable-hdpi xhdpi xxhdpi xxxhdpi 之间的差异

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_enabled="true"android:state_checked="true"android:drawable="@mipmap/a4"/>
<!--这里是图片--><itemandroid:state_enabled="true"android:state_checked="false"android:drawable="@mipmap/a3"/>
</selector>

3.自定义样式的使用
方法一:android:button属性设置为上述的selector

android:button="@drawable/check_btn"

方法二:在style中定义一个属性,然后通过android:style属性设置,先往style添加下述代码:

< style
name=“MyCheckBox”
parent="@android:style/Widget.CompoundButton.CheckBox">
< item name=“android:button”>
@drawable/check_btn
< /item>
< /style>

然后在布局那里

style="@style/MyCheckBox"

ps:如果没有style.xml文件,那就自己新建一个。

修改文字与选择框的距离

android:background="@null"
android:paddingLeft=“40dp”

2.MainActivity.java文件

package com.example.day_17;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;public class ChexBox_MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {private CheckBox checkBox1;private CheckBox checkBox2;private CheckBox checkBox3;private  Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_chex_box__main);checkBox1=findViewById(R.id.first);checkBox2=findViewById(R.id.second);checkBox3=findViewById(R.id.three);button=findViewById(R.id.button1);checkBox1.setOnCheckedChangeListener(this);checkBox2.setOnCheckedChangeListener(this);checkBox3.setOnCheckedChangeListener(this);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {String choose="";if(checkBox1.isChecked()){choose+=checkBox1.getText().toString()+"";}if(checkBox2.isChecked()){choose+=checkBox2.getText().toString()+"";}if(checkBox3.isChecked()){choose+=checkBox3.getText().toString()+"";}Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {String s=buttonView.getText().toString();if(buttonView.isChecked()){Toast.makeText(this,"你选了:"+s,Toast.LENGTH_SHORT).show();}}
}

3.效果展示

Android——基本组件-2相关推荐

  1. android组件化架构 书,Android MVVM组件化架构方案

    MVVMHabitComponent 关于Android的组件化,相信大家并不陌生,网上谈论组件化的文章,多如过江之鲫,然而一篇基于MVVM模式的组件化方案却很少.结合自身的调研和探索,在此分享一篇基 ...

  2. 深圳腾讯内部Jetpack宝典意外流出!极致经典,堪称Android架构组件的天花板

    简介 Jetpack是一套库.工具和指南,可以帮助开发者更轻松地编写优质应用.这些组件可以帮助开发者遵循最佳做法.让开发者摆脱编写样板代码的工作并简化复杂任务,以便开发者将精力集中放在所需的代码上. ...

  3. Android Jetpack组件之Hilt使用

    前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. And ...

  4. Android Jetpack组件App Startup简析

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  5. Android Jetpack组件之WorkManger使用介绍

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  6. Android Jetpack组件之Navigation使用-源码

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  7. Android Jetpack组件之 Room使用-源码

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  8. Android Jetpack组件之 Paging使用-源码

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  9. Android Jetpack组件之 LiveData使用-源码

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  10. Android Jetpack组件之ViewModel使用

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

最新文章

  1. [导入]深入了解OpenOffice.org(二){作者:路广}
  2. useradd或adduser命令
  3. 阿里云,并不是比谁聪明,而是更早面临那些疼痛
  4. 分布式服务框架选型:面对Dubbo,阿里巴巴为什么选择了HSF?
  5. MATLAB实现香农编码
  6. css 下拉 小箭头
  7. Ubuntu安装cuda
  8. mac + win ,用python一行代码批量下载哔哩哔哩视频
  9. 微信小程序一键置顶操作详解:
  10. delphi删除文本内容_文本编辑器EmEditor快捷键指令
  11. 计算某天是某年的第多少天
  12. 鸿蒙系统,鸿蒙app简易登录界面,界面开发教程
  13. cryptographic primitives(密码学原语 )
  14. python中常见的三种选择结构_在Python中,实现多分支选择结构的最佳方法是
  15. 数据标准化的方法与作用
  16. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��'
  17. 程序员的核心竞争力分享
  18. 如何下载jquery的jar包
  19. 大众还是小众?谁在助力《复联3》连破票房纪录
  20. Python-urllib2的使用

热门文章

  1. 中国程序员独闯硅谷,逆袭成美国最佳 CEO,公司市值 160 亿美元!
  2. 微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例
  3. 手指计数——长在身上的计算机
  4. 怎么解决打印机“正在删除-已发送到打印机”打印状态
  5. linux性能监控工具perf,Linux性能分析中常用的工具perf介绍
  6. Linux性能分析工具perf基础使用介绍
  7. 动图图解!既然IP层会分片,为什么TCP层也还要分段?
  8. 小哈智能机器人的功能_小哈智能教育机器人H2产品外观参数说明
  9. 在前端实现excel导入,在线编辑,导出,打印等功能
  10. CCF GitLink开源编程夏令营 · 学生开放报名!