![](app17.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="籃球" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="棒球" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick1"
android:text="Button" />
<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中性" />
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中性" />
</RadioGroup>
</LinearLayout>
package com.example.app17;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton tb1;
CheckBox cb1, cb2, cb3;
RadioButton rb1, rb2, rb3;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb1 = (ToggleButton) this.findViewById(R.id.toggleButton1);
cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
cb2 = (CheckBox) this.findViewById(R.id.checkBox2);
cb3 = (CheckBox) this.findViewById(R.id.checkBox3);
rb1 = (RadioButton) this.findViewById(R.id.radioButton1);
rb2 = (RadioButton) this.findViewById(R.id.radioButton2);
rb3 = (RadioButton) this.findViewById(R.id.radioButton3);
rg = (RadioGroup) this.findViewById(R.id.radioGroup1);
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(MainActivity.this, isChecked ? "開啟" : "關閉",
Toast.LENGTH_SHORT).show();
}
};
tb1.setOnCheckedChangeListener(listener);
CompoundButton.OnCheckedChangeListener listener2 = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layout2);
for (int i=0; i<layout.getChildCount(); i++)
{
if (buttonView != layout.getChildAt(i))
{
RadioButton rb = (RadioButton) layout.getChildAt(i);
rb.setChecked(false);
}
}
}
}
};
rb1.setOnCheckedChangeListener(listener2);
rb2.setOnCheckedChangeListener(listener2);
rb3.setOnCheckedChangeListener(listener2);
RadioGroup.OnCheckedChangeListener listener4 = new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)group.findViewById(checkedId);
Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
rg.setOnCheckedChangeListener(listener4);
}
public void onClick1(View view) {
LinearLayout layout = (LinearLayout) this.findViewById(R.id.layout1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < layout.getChildCount(); i++) {
CheckBox cb = (CheckBox) layout.getChildAt(i);
if (cb.isChecked()) {
if (sb.length() > 0)
sb.append(",");
sb.append(cb.getText().toString());
}
}
Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();
}
}