核取方塊(CheckBox)也是一種提供選擇的見面元件,

不同於單選鈕(RadioButton/RadioGroup)一次只能選取一項,

核取方塊(CheckBox)是可提供複選的選擇元件。

檢查核取方塊(CheckBox)是否被選取只要用其物件呼叫isChecked()方法,

會傳回true或false,表示目前是被勾選或未被勾選。

 

此範例為一個簡單的菜單範例,使用者可以任意選取所要項目,

若點選order按鈕,即列出所點的餐點項目

MainActivity.java

package com.example.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void takeOrder(View v) {
        CheckBox chk;
        String msg="";    
        // 用陣列存放所有 CheckBox 元件的 ID
        int[] id={R.id.chk1, R.id.chk2, R.id.chk3, R.id.chk4}; 

        for(int i:id){    // 以迴圈逐一檢視各 CheckBox 是否被選取
            chk = (CheckBox) findViewById(i);
            if(chk.isChecked())            // 若有被選取
                msg+="\n"+chk.getText();   // 將換行字元及選項文字
        }                                  // 附加到 msg 字串後面

        if(msg.length()>0) // 有點餐
            msg ="your ordeer is "+msg;
        else
            msg ="please take your order";

        // 在文字方塊中顯示點購的項目
        ((TextView) findViewById(R.id.showOrder)).setText(msg);
    }
}



 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >
        
        <CheckBox
            android:id="@+id/chk1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hanburger"
            android:textSize="30dp" />

        <CheckBox
            android:id="@+id/chk2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fries"
            android:textSize="30dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >
        
        <CheckBox
            android:id="@+id/chk3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="coke"
            android:textSize="30dp" />
        
        <CheckBox
            android:id="@+id/chk4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="soup"
            android:textSize="30dp" />

    </LinearLayout>

    <Button
        android:id="@+id/order"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="takeOrder"
        android:text="order"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/showOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:freezesText="true"
        android:textSize="30dp" />

</LinearLayout>

 

arrow
arrow

    Will 發表在 痞客邦 留言(0) 人氣()