一:单选按钮

单选按钮类:RadioButton

android:checked="true"设置默认选中

单选按钮控件通常与RadioGroup搭配使用。 
     RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组。 
     同一按钮组内的单选按钮只能有一个被选中。

二:多选按钮

用法基本与Button相同

CheckBox对象.isChecked()方法可以用来判断复选按钮是否选中

效果图(单选多选写在一个项目里边,用了一个页面跳转):

项目目录:

多选按钮,两种形式

代码:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="${relativePackage}.${activityClass}" ><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="RadioActivity单选" /><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="CheckActivity多选" /></LinearLayout>

MainActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{private Button button1;private Button button2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}@Overridepublic void onClick(View v) {Intent intent = new Intent();switch (v.getId()) {case R.id.button1://跳转页面intent.setClass(MainActivity.this, RadioActivity.class);startActivity(intent);break;case R.id.button2://跳转页面intent.setClass(MainActivity.this, CheckActivity.class);startActivity(intent);default:break;}}
}

activity_radio.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:layout_margin="20sp"tools:context="${relativePackage}.${activityClass}" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><!-- 单选android:checked="true"设置默认选中--><RadioGroupandroid:id="@+id/group1"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButton android:id="@+id/radio1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="男"/><RadioButton android:id="@+id/radio2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"/></RadioGroup><!-- 分界线 --><Viewandroid:layout_width="match_parent"android:layout_height="2sp"android:background="@android:color/holo_blue_dark"android:layout_marginTop="10sp"android:layout_marginBottom="10sp" /><TextView android:id="@+id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"android:text="你吃饭了吗?"/><RadioGroupandroid:id="@+id/group2"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButton android:id="@+id/radio3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="吃了"/><RadioButton android:id="@+id/radio4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="没吃"/></RadioGroup></LinearLayout>

RadioActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class RadioActivity extends Activity implements OnCheckedChangeListener {private RadioGroup group1;private RadioGroup group2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio);group1 = (RadioGroup) findViewById(R.id.group1); group2 = (RadioGroup) findViewById(R.id.group2); group1.setOnCheckedChangeListener(this);group2.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {//显示值的几种方法//checkedId选中RadioButton的id/*switch (checkedId) {case R.id.radio1:Toast.makeText(this, "男", Toast.LENGTH_LONG).show();break;case R.id.radio2:Toast.makeText(this, "女", Toast.LENGTH_LONG).show();break;case R.id.radio3:Toast.makeText(this, "吃了", Toast.LENGTH_LONG).show();break;case R.id.radio4:Toast.makeText(this, "没吃", Toast.LENGTH_LONG).show();break;default:break;}*///找到点击的RadioButton//RadioButton radio = (RadioButton) findViewById(checkedId);//取出RadioButton中的值//String str = radio.getText().toString();//弹框显示选中的值//Toast.makeText(this, str, Toast.LENGTH_LONG).show();//两组数据同时显示//根据RadioGroup取出数据,没有选中返回-1String str = "";int buttonId = group1.getCheckedRadioButtonId();if(buttonId != -1){RadioButton radio = (RadioButton) findViewById(buttonId);str = "你的性别是" + radio.getText().toString();          }else{str = "你没有选择性别";}buttonId = group2.getCheckedRadioButtonId();if(buttonId != -1){RadioButton radio = (RadioButton) findViewById(buttonId);str += ",   你吃饭了吗?"+radio.getText().toString();}Toast.makeText(this, str, Toast.LENGTH_LONG).show();}
}

activity_check.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="${relativePackage}.${activityClass}" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="选择所学课程:" /><CheckBoxandroid:id="@+id/check1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="HTML" /><CheckBoxandroid:id="@+id/check2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C" /><CheckBoxandroid:id="@+id/check3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="php" /><CheckBoxandroid:id="@+id/check4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="java" /><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="提交" /></LinearLayout>

CheckActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;public class CheckActivity extends Activity {private CheckBox check1;private CheckBox check2;private CheckBox check3;private CheckBox check4;private Button button1;private OnCheckedChangeListener listenter = new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//选中多选框CheckBox check = (CheckBox)buttonView;//取出当前勾选值String str = check.getText().toString();//判断是否勾选状态if(isChecked){str = "你学了"+str;}else{str = "你没学"+str;}Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_check);check1 = (CheckBox) findViewById(R.id.check1);check2 = (CheckBox) findViewById(R.id.check2);check3 = (CheckBox) findViewById(R.id.check3);check4 = (CheckBox) findViewById(R.id.check4);button1 = (Button) findViewById(R.id.button1);//多选框点击事件/*check1.setOnCheckedChangeListener(listenter);check2.setOnCheckedChangeListener(listenter);check3.setOnCheckedChangeListener(listenter);check4.setOnCheckedChangeListener(listenter);*///提交按钮点击事件button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String str = "我学过了";boolean f = false;if(check1.isChecked()){str += check1.getText()+",";f = true;}if(check2.isChecked()){str += check2.getText()+",";f = true;}if(check3.isChecked()){str += check3.getText()+",";f = true;}if(check4.isChecked()){str += check4.getText()+",";f = true;}if(f){str = str.substring(0, str.length()-1);}Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();}});}
}

Android——单选多选按钮的使用详解相关推荐

  1. 数据列表 多选 html,Vue多选列表组件深入详解

    这篇文章主要介绍了Vue多选列表组件深入详解,这个是vue的基本组件,有需要的同学可以研究下 多选列表 (Multi-Select) 是一种将所有选项列出,并允许用户利用 Ctrl/Shift 键进行 ...

  2. 融云android聊天界面,Android 融云IM集成以及使用详解(一)

    Android 融云IM集成以及使用详解(一) 集成 1.具体的集成步骤就不在详细介绍,我们只说干货,附上融云IM官方文档地址,里面有更为详细的集成介绍 https://www.rongcloud.c ...

  3. Android 融云IM集成以及使用详解(二)

    Android 融云IM集成以及使用详解(二) 上篇讲解了集成和好友列表和消息记录的使用,这篇将讲解聊天界面和群聊界面的使用 先附上一张效果图 先介绍布局文件 <LinearLayout xml ...

  4. Android进阶笔记:Messenger源码详解

    Messenger可以理解为一个是用于发送消息的一个类用法也很多,这里主要分析一下再跨进程的情况下Messenger的实现流程与源码分析.相信结合前面两篇关于aidl解析文章能够更好的对aidl有一个 ...

  5. android中怎么网络判断,Android中判断网络是否连接实例详解

    Android中判断网络是否连接实例详解 在android中,如何监测网络的状态呢,这个有的时候也是十分重要的,方法如下: public class ConnectionDetector { priv ...

  6. 【转】Android APK反编译就这么简单 详解(附图)

     转自:http://blog.csdn.net/vipzjyno1/article/details/21039349/ [置顶] Android APK反编译就这么简单 详解(附图) 分类: and ...

  7. android组件用法说明,Android第三方控件PhotoView使用方法详解

    Android第三方控件PhotoView使用方法详解 发布时间:2020-10-21 15:06:09 来源:脚本之家 阅读:74 作者:zhaihaohao1 PhotoView的简介: 这是一个 ...

  8. Android 颜色渲染(九) PorterDuff及Xfermode详解

    Android 颜色渲染(九)  PorterDuff及Xfermode详解 之前已经讲过了除ComposeShader之外Shader的全部子类, 在讲ComposeShader(组合渲染)之前,  ...

  9. android 截图 listview,Android屏幕及view的截图实例详解

    Android屏幕及view的截图实例详解 屏幕可见区域的截图 整个屏幕截图的话可以用View view = getWindow().getDecorView(); public static Bit ...

最新文章

  1. geoip2 php,Geoip geoip-api-php 库包使用 – 通过ip 找到国家
  2. Java Web学习计划
  3. python打包exe 之打包sklearn模型中的各种坑及其解决方法。
  4. java中单列集合的根接口是_java 单列集合总结
  5. 机器学习第九篇:详解Adaboost算法
  6. ssl1615-Frogger【图论,最小生成树,并查集】
  7. server sql top速度变慢解决方案_SQL Server数据库查询速度慢的原因和解决方法
  8. sqldeveloper创建账号_用oralce 自带工具sql developer 创建表空间,用户,权限
  9. 给定一个年份,判断是不是闰年
  10. STM32 - 定时器的设定 - 基础-04 - 输出波形控制 - PWM 模式
  11. 负载均衡集群HAProxy安装篇
  12. 关于div的定位属性问题
  13. android中白色怎么表示,android – 将位图中特定颜色以外的所有颜色转换为白色...
  14. IIS添加对ashx文件的支持
  15. 凸包算法(Convex Hull Algorithm)
  16. 员工申请加薪无望辞职,老板:不知感恩
  17. 要做好云计算所需要的成本,主要分为哪六大成本?
  18. 无损数据压缩算法c语言,C语言实现无损压缩算法
  19. 软工作业 4:结对项目之词频统计——基本功能
  20. 哪种程序员最挣钱?平均月薪30.8K,网友说这是掌握世界的技术

热门文章

  1. qt实现汽车销售管理系统(一)-- 登录界面的实现
  2. 【爬虫】使用requests爬取英雄联盟英雄皮肤
  3. 关于win8应用商店的tiny troopers游戏的破解
  4. EKS集群NLB配置白名单访问
  5. LINUX安装rhel字符界面教程
  6. python爬取论坛图片_[python爬虫] Selenium定向爬取虎扑篮球海量精美图片
  7. word自定义插入图片快捷键
  8. PeekMessage完美解决MFC主界面无响应
  9. 小悦2013功能清单
  10. 怎样避免衣服虫咬、泛黄、发霉、变形