Android控件之CheckBox、RadioButton用法实例分析
作者:Ruthless 发布时间:2021-06-01 06:31:26
标签:Android,控件,CheckBox,RadioButton
本文实例讲述了Android控件之CheckBox、RadioButton用法。分享给大家供大家参考。具体如下:
CheckBox和RadioButton控件都只有选中和未选中状态,不同的是RadioButton是单选按钮,需要编制到一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。
以下为CheckBox和RadioButton常用方法及说明
以下为单选按钮和复选按钮的使用方法
目录结构:
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- RadioButton控件演示 -->
<ImageView android:id="@+id/imageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_on"
android:layout_gravity="center_horizontal" />
<RadioGroup android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<RadioButton android:id="@+id/on"
android:text="开灯"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton android:id="@+id/off"
android:text="关灯"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<!-- CheckBox控件演示 -->
<ImageView android:id="@+id/imageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_on"
android:layout_gravity="center_horizontal" />
<CheckBox android:id="@+id/checkBox"
android:text="开灯"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
CbRbActivity类:
package com.ljq.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CbRbActivity extends Activity {
private ImageView imageView01=null;
private ImageView imageView02=null;
private CheckBox checkBox=null;
private RadioButton on=null;//开灯
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView01=(ImageView)findViewById(R.id.imageView01);
imageView02=(ImageView)findViewById(R.id.imageView02);
checkBox=(CheckBox)findViewById(R.id.checkBox);
on=(RadioButton)findViewById(R.id.on);
on.setOnCheckedChangeListener(listener);
checkBox.setOnCheckedChangeListener(listener);
}
OnCheckedChangeListener listener=new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(buttonView instanceof RadioButton){
imageView01.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
}else if(buttonView instanceof CheckBox){
checkBox.setText(isChecked?"开灯":"关灯");
imageView02.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
}
}
};
}
运行结果:
希望本文所述对大家的Android程序设计有所帮助。


猜你喜欢
- 本文介绍了eclipse下搭建hibernate5.0环境的步骤,分享给大家,具体如下:hibernate引入的jar包:hibernate
- 在使用 springboot 或者 springcloud 开发的时候,通常为了保证系统的安全性,配置文件中的密码等铭感信息都会进行加密处理
- 这篇文章主要介绍了通过实例解析JMM和Volatile底层原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 一,哈希表(Hashtable)简述在.NET Framework中,Hashtable是System.Collections命名空间提供的
- Android开发文档上专门有一小节解释这个问题。简单来说,Activity是负责与用户交互的最主要机制,任何“设置”(Configurat
- 1、创建在class块外面:class Test{}/** 我是main入口函数 **/fun main(args: Array<St
- 1、Fragment的静态使用Fragment是作为Activity的UI的一部分,它内嵌在Activity中,多个Fragment可以把一
- 一、前端搭建1、前端用到js:uploadify(下载地址:http://www.uploadify.com/download/)、laye
- mybatis-plus 新增/修改 自动填充指定字段1.需要修改的字段在模型类上添加@TableField(fill = FieldFil
- 前言在上一篇中,我们介绍了使用位运算实现加法和减法运算,接下来本文主要介绍如何用位运算实现乘法运算,在实现乘法时要用位运算实现,并且不能出现
- 标识接口是没有任何方法和属性的接口。标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型。标接口在Java语言中
- 前言今天我们继续聊聊在SprinBoot中如何集成参数校验Validator,以及参数校验的高阶技巧(自定义校验,分组校验)。&ld
- 本文实例为大家分享了Android弹出菜单效果的具体代码,供大家参考,具体内容如下功能描述:用户单击按钮弹出菜单。当用户选择一个菜单项,会触
- 本文实例为大家分享了Android仿京东分类效果展示的具体代码,供大家参考,具体内容如下1.写一个fragmentimport androi
- 在上篇文章给大家介绍了WebService教程详解(一)使用工具的原因:1、 使用工具可以更好的了解WebService请求的过程 2、 使
- 本文实例讲述了C#编程之事务用法。分享给大家供大家参考,具体如下:ado.net2.0的SqlTransaction使用方法/////ado
- jpa之动态插入与修改(重写save)1.动态插入@Data@Entity@DynamicInsert@Table(name = "
- MyBatis 是一款常用的持久层框架,使得程序能够以调用方法的方式执行某个指定的SQL,将执行SQL的底层逻辑进行封装。多数与Spring
- 日常工作中,不管你是写Unit Test,还是采用TDD的编程方式进行开发,都会遇到断言。而断言的风格常见的会有Assert、BDD风格,对
- 1.with 函数首先先从with函数开始,with函数接受两个参数,第一个参数可以是一个任意类型的对象,第二个参数是一个Lambda表达式