gpt4 book ai didi

Android控件之CheckBox、RadioButton用法实例分析

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Android控件之CheckBox、RadioButton用法实例分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了android控件之checkbox、radiobutton用法。分享给大家供大家参考。具体如下:

checkbox和radiobutton控件都只有选中和未选中状态,不同的是radiobutton是单选按钮,需要编制到一个radiogroup中,同一时刻一个radiogroup中只能有一个按钮处于选中状态.

以下为checkbox和radiobutton常用方法及说明 。

Android控件之CheckBox、RadioButton用法实例分析

以下为单选按钮和复选按钮的使用方法 。

目录结构:

Android控件之CheckBox、RadioButton用法实例分析

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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控件之CheckBox、RadioButton用法实例分析

希望本文所述对大家的android程序设计有所帮助.

最后此篇关于Android控件之CheckBox、RadioButton用法实例分析的文章就讲到这里了,如果你想了解更多关于Android控件之CheckBox、RadioButton用法实例分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com