MainActivity:
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ButtonActivity extends Activity {
private TextView mTextView01; // 宣告顯示按鈕按下的資訊 TextView
//ButtonActivity主程式
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_activity);
//建製顯示按鈕按下的資訊 TextView
mTextView01 = (TextView)findViewById(R.id.TextView01);
//建製按下一般按鈕【Button】-文字印在按鈕上
Button mButton01 = (Button)findViewById(R.id.Button01);
mButton01.setOnClickListener(new Button01OnClickListener());
//建製按下ON/OFF交替切換按鈕【ToggleButton】
ToggleButton mToggleButton01 = (ToggleButton)findViewById(R.id.ToggleButton01);
mToggleButton01.setOnClickListener(new ToggleButton01OnClickListener());
//建製按下一般按鈕【ImageButton】-影像印在按鈕上
ImageButton mImageButton01 = (ImageButton)findViewById(R.id.ImageButton01);
mImageButton01.setOnClickListener(new ImageButton01OnClickListener());
//建製按下多選核選盒【CheckBox】
Button mButton02 = (Button)findViewById(R.id.Button02);
mButton02.setOnClickListener(new Button02OnClickListener());
//建製按下圈選(單選)按鈕【RadioButton】
Button mButton03 = (Button)findViewById(R.id.Button03);
mButton03.setOnClickListener(new Button03OnClickListener());
}
// 監聽器 : 按下一般按鈕【Button】
private class Button01OnClickListener implements OnClickListener {
public void onClick(View v) {
String msg = "按下Button-【按我!】按鈕";
mTextView01.setText(msg);
}
}
// 監聽器 : 按下ON/OFF交替切換按鈕【ToggleButton】
private class ToggleButton01OnClickListener implements OnClickListener {
public void onClick(View v) {
String msg = "按下【ToggleButton】按鈕:";
mTextView01.setText(msg + ((ToggleButton)v).getText());
}
}
// 監聽器 : 按下一般按鈕【ImageButton】
private class ImageButton01OnClickListener implements OnClickListener {
public void onClick(View v) {
String msg = "按下【ImageButton】按鈕";
mTextView01.setText(msg);
}
}
// 監聽器 : 按下多選核選盒【CheckBox】
private class Button02OnClickListener implements OnClickListener {
public void onClick(View v) {
// 建立 English 核選盒(CheckBox)
CheckBox mCheckBox01 = (CheckBox)findViewById(R.id.CheckBox01);
// 建立 Japanese 核選盒(CheckBox)
CheckBox mCheckBox02 = (CheckBox)findViewById(R.id.CheckBox02);
String msg = "按下【CheckBox】按鈕: ";
if (mCheckBox01.isChecked()) { // 判斷選取的是否是 English
msg += mCheckBox01.getText() +",";
}
if (mCheckBox02.isChecked()) { // 判斷選取的是否是 Japanese
msg += mCheckBox02.getText() +",";
}
mTextView01.setText(msg);
}
}
// 監聽器 : 按下圈選(單選)按鈕【RadioButton】
private class Button03OnClickListener implements OnClickListener {
public void onClick(View v) {
// 建立 Man 圈選按鈕(RadioButton)
RadioButton mRadioButton01 = (RadioButton)findViewById(R.id.RadioButton01);
// 建立 Female 圈選按鈕(RadioButton)
RadioButton mRadioButton02 = (RadioButton)findViewById(R.id.RadioButton02);
String msg = "按下【RadioButton】按鈕: ";
if (mRadioButton01.isChecked()) {
msg += mRadioButton01.getText(); // 判斷選取的是否是 Man
}
if (mRadioButton02.isChecked()) {
msg += mRadioButton02.getText(); // 判斷選取的是否是 Female
}
mTextView01.setText(msg);
}
}
}
button.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center"
android:orientation="vertical">
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_pushme"/>
<ToggleButton android:id="@+id/ToggleButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/title"/>
<CheckBox android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_check_eg"/>
<CheckBox android:id="@+id/CheckBox02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_check_jp"/>
<Button android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_checkbox"/>
<RadioGroup android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--"horizontal是把RedioButton改成水平排列">
<RadioButton android:text="@string/app_man"
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton android:text="@string/app_female"
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
</RadioGroup>
<Button android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_radiobutton"/>
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示按鈕的結果"/>
</TableLayout>