1. 程式人生 > >Android之複選按鈕、單選按鈕、開關按鈕

Android之複選按鈕、單選按鈕、開關按鈕

Android的複選按鈕、單選按鈕、開關按鈕是經常使用的幾個元件。下面給出一個例子

<?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" >

    <RadioGroup                        //單選按鈕組
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton                     //單選按鈕
            android:id="@+id/rbm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"        //預設選中
            android:text="man" />

        <RadioButton
            android:id="@+id/rbw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="woman" />

        <RadioButton
            android:id="@+id/rbn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="no" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox                         //複選框
            android:id="@+id/cba"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android" />

        <CheckBox
            android:id="@+id/cbj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="java" />

        <CheckBox
            android:id="@+id/cbn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="net" />
    </LinearLayout>

    <ToggleButton                          //開關按鈕
        android:id="@+id/tb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="關燈"
        android:textOn="開燈" />

</LinearLayout>

對應的Java程式碼。要注意的是他們要實現的監聽介面不同。

package org.zqy.viewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class CheckActivity extends Activity implements
		CompoundButton.OnCheckedChangeListener {

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

		RadioGroup rg = (RadioGroup) findViewById(R.id.rg); // 得到單選按鈕組
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { // 設定選擇監聽

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {// 根據checkedId獲得選擇的按鈕
				RadioButton rb = (RadioButton) findViewById(checkedId);
				Toast.makeText(CheckActivity.this, rb.getText(),
						Toast.LENGTH_SHORT).show();
			}
		});
		// 得到複選按鈕框
		CheckBox cba = (CheckBox) findViewById(R.id.cba);
		CheckBox cbj = (CheckBox) findViewById(R.id.cbj);
		CheckBox cbn = (CheckBox) findViewById(R.id.cbn);
		cba.setOnCheckedChangeListener(this);// 設定選擇監聽
		cbj.setOnCheckedChangeListener(this);
		cbn.setOnCheckedChangeListener(this);

		final ToggleButton tb = (ToggleButton) findViewById(R.id.tb);
		tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			// 注意,此處實現的是CompoundButton的介面
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			}
		});
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			Toast.makeText(CheckActivity.this, buttonView.getText(),
					Toast.LENGTH_SHORT).show();
		}
	}
}