Android控件之CheckBox、RadioButton探究

简介: CheckBox和RadioButton控件都只有选中和未选中状态,不同的是RadioButton是单选按钮,需要编制到一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态

以下为CheckBox和RadioButton常用方法及说明


image.png

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



publicclass CbRbActivity extends Activity {
   
private ImageView imageView01=null;
   
private ImageView imageView02=null;
   
private CheckBox checkBox=null;
   
private RadioButton on=null;//开灯
   
   @Override
   
publicvoid 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(){

       
publicvoid onCheckedChanged(CompoundButton buttonView,
               
boolean isChecked) {
           
if(buttonView instanceof RadioButton){
               imageView01.setImageResource(isChecked
?R.drawable.bulb_on:R.drawable.bulb_off);
           }
elseif(buttonView instanceof CheckBox){
               checkBox.setText(isChecked
?"开灯":"关灯");
               imageView02.setImageResource(isChecked
?R.drawable.bulb_on:R.drawable.bulb_off);
           }
       }
   };
}

目录
相关文章
|
7月前
|
XML 搜索推荐 Android开发
Android改变进度条控件progressbar的样式(根据源码修改)
本文介绍了如何基于Android源码自定义ProgressBar样式。首先分析了系统源码中ProgressBar样式的定义,发现其依赖一张旋转图片实现动画效果。接着分两步指导开发者实现自定义:1) 模仿源码创建一个旋转动画XML文件(放置在drawable文件夹),修改图片为自定义样式;2) 在UI控件中通过`indeterminateDrawable`属性应用该动画。最终实现简单且个性化的ProgressBar效果,附带效果图展示。
507 2
|
7月前
|
Android开发
Android控件样式的抽取(小提及快捷方式)
在Android开发中,若多个控件样式重复,可抽取公共部分以简化代码。例如对EditText提取样式,通过编辑`styles.xml`实现复用。为提高效率,Android Studio提供自动提取Style功能:右键点击控件样式选项,选择“Style...”,勾选需要提取的属性后确认,即可快速生成样式代码,显著提升开发便利性。
241 2
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
186 3
|
XML 存储 Java
浅谈Android的TextView控件
浅谈Android的TextView控件
141 0
|
XML Android开发 数据格式
Android自定义控件(十二)——自定义属性及应用
Android自定义控件(十二)——自定义属性及应用
300 0
Android自定义控件(十二)——自定义属性及应用
|
XML Android开发 数据格式
Android开发技巧——自定义控件之自定义属性
Android开发技巧——自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码。 上一篇讲了如何通过xml把几个控件组织起来,并继承某个ViewGroup子类,把它们封装起来使用。
1282 0
|
XML 前端开发 Android开发
android自定义控件(理论知识学习 +自定义属性的讲解)
View树和UI界面架构图   UI界面架构图: android视图最外层是一个window对象。 phoneWindow来实现。 phoneWindow将一个decorView作为整个布局的根view. 屏幕分为TitleView和ContentView. ContentView的根布局为framelayout.   view的测量: view的测量通过onMesure()来进行的: onMesure用来确定视图大小和位置。
1216 0
|
Android开发
Android自定义控件及自定义属性
Android自定义控件及自定义属性 自定义控件 创建自定义控件 自定义一个类,继承View 继承View还是哪个类,取决于你要实现一个什么样的控件 如...
1359 0

热门文章

最新文章