安卓開發_複選按鈕控制項(CheckBox)的簡單使用

來源:互聯網
上載者:User

標籤:

複選按鈕 即可以選擇若干個選項,與選項按鈕不同的是,複選按鈕的表徵圖是方塊,選項按鈕是圓圈

複選按鈕用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     @Override23     protected void onCreate(Bundle savedInstanceState) {24         // TODO Auto-generated method stub25         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             @Override42             public void onClick(View v) {43                 // TODO Auto-generated method stub44                 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     @Override59     public void onCheckedChanged(CompoundButton arg0, boolean arg1) {60         // TODO Auto-generated method stub61         62     }63     64 }

可以看到,代碼是很簡單的,只有一個方法需要學習

checkbox_1.isChecked()

返回checkbox_1對應的複選按鈕控制項是否被選中

 



安卓開發_複選按鈕控制項(CheckBox)的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.