package com.duoguo.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* 1、CheckBox的使用; 2、RadioButton的使用; 3、Toast的使用。
*
* @author shyboy([email protected])
*
*/
public class ButtonActivityActivity extends Activity {
private RadioGroup genderRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private CheckBox footballCheckBox;
private CheckBox basketballCheckBox;
private CheckBox volleyballCheckBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genderRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroupId);
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);
footballCheckBox = (CheckBox) findViewById(R.id.footballCheckBoxId);
basketballCheckBox = (CheckBox) findViewById(R.id.basketballCheckBoxId);
volleyballCheckBox = (CheckBox) findViewById(R.id.volleyballCheckBoxId);
// 为RadioGroup添加单击监听事件
genderRadioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (maleRadioButton.getId() == checkedId)// 当单选按钮被选中时
{
System.out.println("male");
Toast.makeText(ButtonActivityActivity.this,
"您选择的是" + maleRadioButton.getText(),
Toast.LENGTH_LONG).show();
} else if (femaleRadioButton.getId() == checkedId) {
System.out.println("female");
Toast.makeText(ButtonActivityActivity.this,
"您选择的是:" + femaleRadioButton.getText(),
Toast.LENGTH_LONG).show();
}
}
});
// 为CheckBox添加单击监听事件
footballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("football");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + footballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + footballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
basketballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("basketball");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + basketballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + basketballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
volleyballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("volleyball");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + volleyballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + volleyballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:id="@+id/genderRadioGroupId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/maleRadioButtonId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/male" />
<RadioButton android:id="@+id/femaleRadioButtonId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<CheckBox android:id="@+id/footballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/football" />
<CheckBox android:id="@+id/basketballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/basketball" />
<CheckBox android:id="@+id/volleyballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/volleyball" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ButtonActivity</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="football">足球</string>
<string name="basketball">篮球</string>
<string name="volleyball">排球</string>
</resources>