安卓開發_復選按鈕控件(CheckBox)的簡單使用


復選按鈕 即可以選擇若干個選項,與單選按鈕不同的是,復選按鈕的圖標是方塊,單選按鈕是圓圈

復選按鈕用CheckBox表示,CheckBox是Button的子類,支持使用Button的所有屬性

一、由於復選框可以選中多項,所有為了確定用戶是否選擇了某一項,還需要為每一個選項添加setOnCheckedChangeListener事件監聽

例如:

為id為like1的復選按鈕添加狀態改變事件監聽,代碼如下

 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     });

二、使用示例

先看布局文件

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

 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對應的復選按鈕控件是否被選中

 

效果圖:




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM