前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓开发_复选按钮控件(CheckBox)的简单使用

安卓开发_复选按钮控件(CheckBox)的简单使用

作者头像
听着music睡
发布2018-05-18 12:25:06
1.5K0
发布2018-05-18 12:25:06
举报
文章被收录于专栏:Android干货Android干货

复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈

复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性

一、由于复选框可以选中多项,所有为了确定用户是否选择了某一项,还需要为每一个选项添加setOnCheckedChangeListener事件监听

例如:

为id为like1的复选按钮添加状态改变事件监听,代码如下

代码语言:javascript
复制
 1 final CheckBox like1 = (CheckBox)findViewById(R.id.like1);
 2     //监听事件
 3     
 4     like1.setOnCheckedChangeListener(new OnCheckedChangeListener()){
 5         
 6     @Override
 7     public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
 8         // TODO Auto-generated method stub
 9         if(like1.isChecked())
10             like1.getText();
11         }
12     });

二、使用示例

先看布局文件

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择您的爱好"
        android:textSize="19dp"
        />
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id_checkbox_1"
        android:text="音乐"
        />
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id_checkbox_2"
        android:text="美术"
        />
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id_checkbox_3"
        android:text="体育"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:id="@+id/btn_checkbox_tijiao" 
        />
</LinearLayout>

效果图:

再看JAVA文件

代码语言:javascript
复制
 1 package base_ui;
 2 
 3 import com.example.allcode.R;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.CheckBox;
11 import android.widget.Checkable;
12 import android.widget.CompoundButton;
13 import android.widget.RadioGroup.OnCheckedChangeListener;
14 import android.widget.Toast;
15 
16 public class Ui_CheckBox extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener{
17     private Button tijiao;
18     private CheckBox checkbox_1;
19     private CheckBox checkbox_2;
20     private CheckBox checkbox_3;
21     private OnCheckedChangeListener checkbox_listen ;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         // TODO Auto-generated method stub
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.base_ui_checkbox);
27     
28         tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao);
29         
30         checkbox_1 = (CheckBox) findViewById(R.id.id_checkbox_1);
31         checkbox_2 = (CheckBox) findViewById(R.id.id_checkbox_2);
32         checkbox_3 = (CheckBox) findViewById(R.id.id_checkbox_3);
33         tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao);
34         
35         checkbox_1.setOnCheckedChangeListener(this);
36         checkbox_2.setOnCheckedChangeListener(this);
37         checkbox_3.setOnCheckedChangeListener(this);
38         
39         tijiao.setOnClickListener(new OnClickListener() {
40             
41             @Override
42             public void onClick(View v) {
43                 // TODO Auto-generated method stub
44                 String str="";   //存放选中的选项的值
45                 if(checkbox_1.isChecked())
46                     str+=checkbox_1.getText().toString()+" ";
47                 if(checkbox_2.isChecked())
48                     str+=checkbox_2.getText().toString()+" ";
49                 if(checkbox_3.isChecked())
50                     str+=checkbox_3.getText().toString()+" ";
51                 Toast.makeText(Ui_CheckBox.this, "您选择的喜欢的爱好为:"+str, 1).show();
52                 
53                 
54             }
55         });
56     }
57     //监听事件
58     @Override
59     public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
60         // TODO Auto-generated method stub
61         
62     }
63     
64 }

可以看到,代码是很简单的,只有一个方法需要学习

checkbox_1.isChecked()

返回checkbox_1对应的复选按钮控件是否被选中

效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-07-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档