1.RadioButton

(1)介绍

(2)单选按钮点击事件的用法

(3)RadioButton与RadioGroup配合使用实现单选题功能

(4)xml布局及使用

<?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"> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您最喜欢的城市是" /> <RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="北京" /> <RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上海" /> <RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="广州" /> <RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="杭州" />
</RadioGroup> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>

2.复选框(CheckBox)

(1)介绍

(2)xml文件

<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你最喜欢的运动是"/> <CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乒乓球" /> <CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="羽毛球" /> <CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="排球" /> <CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="足球" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />

(3)java后台

对应工程名:test23

package com.lucky.test23;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button button1;
Button button2;
RadioGroup radioGroup;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2);
radioGroup=findViewById(R.id.radiogroup);
checkBox1=findViewById(R.id.checkBox);
checkBox2=findViewById(R.id.checkBox2);
checkBox3=findViewById(R.id.checkBox3);
checkBox4=findViewById(R.id.checkBox4); //绑定按钮点击事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i <radioGroup.getChildCount(); i++) { //radioGroup.getChildCount()获取子容器数量
RadioButton radioButton= (RadioButton) radioGroup.getChildAt(i); //判断按钮是否被选中
if(radioButton.isChecked()){
String str=radioButton.getText().toString();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
break;
}
}
}
}); button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将变量放入数组中便于取用
CheckBox[] cbox={checkBox1,checkBox2,checkBox3,checkBox4};
String str="";
//遍历数组,判断各个复选框的选中情况
for (int i = 0; i <cbox.length ; i++) {
if(cbox[i].isChecked()){
str=str+cbox[i].getText().toString();
}
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
}

 3.效果图

Android 单选按钮(RadioButton)和复选框(CheckBox)的使用的更多相关文章

  1. 3.Android之单选按钮RadioGroup和复选框Checkbox学习

    单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...

  2. 安卓开发:UI组件-RadioButton和复选框CheckBox

    2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...

  3. 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

    原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...

  4. android 中单选和复选框监听操作

    单选按钮RadioGroup.复选框CheckBox都有OnCheckedChangeListener事件,我们一起了解一下. package com.genwoxue.oncheckedchange ...

  5. 关于bootstrap--表单(下拉<select>、输入框<input>、文本域<textare>复选框<checkbox>和单选按钮<radio>)

    html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...

  6. [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果

    图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...

  7. css3美化复选框checkbox

     两种美化效果如下图: 代码(html) <div id="main"> <h2 class="top_title">使用CSS3美化复 ...

  8. 复选框(checkbox)、单选框(radiobox)的使用

    复选框(checkbox).单选框(radiobox)的使用 复选框: HTML: // 复选框 <input type="checkbox" name="chec ...

  9. php 判断复选框checkbox是否被选中

    php 判断复选框checkbox是否被选中   复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...

随机推荐

  1. Spring总结八:jdbcTemplate的简单使用

    简介: Srping毕竟是一站式框架,所以也有操作数据库的东西,那就是jdbcTemplate,介绍一下jdbcTemplate的简单使用. 除了要引入必要的包以外,还需要引入 spring-jdbc ...

  2. java基础之JDBC六:DBCP 数据库连接池简介

    我们之前写的代码中的数据库连接每次都是自己创建,用完以后自己close()销毁的,这样是很耗费资源的,所以我们引入DBCP DBCP简介 概述: Data Base Connection Pool, ...

  3. 10-编译PHP并与nginx整合

    nginx的URL重写.nginx+PHP的配置也是不可不学的部分.PHP自己手动编译,mysql就自己yum了. yum install 安装mysql 同时进行php的编译,手动编译php 这么多 ...

  4. mfs教程(四)

    mfs文件系统(四) MooseFS  维护 一.启动MooseFS集群 最安全的启动MooseFS集群(避免任何读或写的错误数据或类似的问题)的方式是按照以下命令步骤: 1.启动mfsmaster进 ...

  5. PHP 商城无限极分类

    无限极分类,用的是递归,在外部调用fen()方法即可 index是刚开始寻找的顶级分类,suo是为了在前端展示的时候缩进 ,$suo=){ 一个数组用来返回的 $t=[]; 这是查询数据库的所有内容 ...

  6. 661. Image Smoother色阶中和器

    [抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...

  7. 997D Cycles in product

    传送门 题目大意 https://www.luogu.org/problemnew/show/CF997D 分析 我们发现两棵树互不相关 于是我们可以分别求出两棵树的信息 我们点分,人啊按后设f[i] ...

  8. python23种设计模式

      第一篇 Python与设计模式:前言 第二篇(23种设计模式) 创建类设计模式(5种) 单例模式.工厂模式.简单工厂模式.抽象工厂模式.建造者模式.原型模式 结构类设计模式(7种) 代理模式.装饰 ...

  9. 编写高质量代码改善C#程序的157个建议——建议13: 为类型输出格式化字符串

    建议13: 为类型输出格式化字符串 有两种方法可以为类型提供格式化的字符串输出.一种是意识到类型会产生格式化字符串输出,于是让类型继承接口IFormattable.这对类型来 说,是一种主动实现的方式 ...

  10. 设计模式09: Decorator 装饰模式(结构型模式)

    Decorator 装饰模式(结构型模式) 子类复子类,子类何其多加入我们需要为游戏中开发一种坦克,除了不同型号的坦克外,我们还希望在不同场合中为其增加以下一种多种功能:比如红外线夜视功能,比如水路两 ...