[Android开发学习26]界面控件之选项组件(RadioGroup和CheckBox)
一、基础知识:
单项选择组件 : RadioGroup 互相排斥
多项选择组件 : CheckBox 彼此独立
二、代码展示:
1."Activity_08\src\yan\activity_08\MainActivity.java"
[java]
package yan.activity_08;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
//import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup sixRadioGroup;
private RadioButton femaleRadioButton;
private RadioButton maleRadioButton;
private CheckBox swimCheckBox;
private CheckBox runCheckBox;
private CheckBox readCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sixRadioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);
femaleRadioButton = (RadioButton)findViewById(R.id.myRadioButton1);
maleRadioButton = (RadioButton)findViewById(R.id.myRadioButton2);
swimCheckBox = (CheckBox)findViewById(R.id.swim);
runCheckBox = (CheckBox)findViewById(R.id.run);
readCheckBox = (CheckBox)findViewById(R.id.read);
class RadioGroupCheckBoxListener implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == femaleRadioButton.getId())
{
Toast.makeText(MainActivity.this, R.string.female_y_note, Toast.LENGTH_SHORT).show();
}else if(checkedId == maleRadioButton.getId())
{
Toast.makeText(MainActivity.this, R.string.male_y_note, Toast.LENGTH_SHORT).show();
}
}
}
class SwimCheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, R.string.swim_y_note, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, R.string.swim_n_note, Toast.LENGTH_SHORT).show();
}
}
}
class RunCheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, R.string.run_y_note, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, R.string.run_n_note, Toast.LENGTH_SHORT).show();
}
}
}
class ReadCheckBoxListener implements OnCheckedChangeListener{
&n
补充:移动开发 , Android ,