packagecom.example.checkbox;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.widget.CheckBox;importandroid.widget.CompoundButton;importandroid.widget.Toast;importandroid.widget.CompoundButton.OnCheckedChangeListener;importandroid.widget.TextView;publicclassMainActivityextendsActivityimplementsOnCheckedChangeListener{privateCheckBoxcb1,cb2,cb3;privateTextViewtv;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);cb1=(CheckBox)findViewById(R.id.cb1);cb2=(CheckBox)findViewById(R.id.cb2);cb3=(CheckBox)findViewById(R.id.cb3);cb1.setOnCheckedChangeListener(this);cb2.setOnCheckedChangeListener(this);cb3.setOnCheckedChangeListener(this);tv=(TextView)findViewById(R.id.tv);}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main,menu);returntrue;}@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){// TODO Auto-generated method stubif(cb1==buttonView||cb2==buttonView||cb3==buttonView){if(isChecked){showToast(buttonView.getText()+"选中");}else{showToast(buttonView.getText()+"取消");}}}publicvoidshowToast(Stringstr){Toast.makeText(this,str,Toast.LENGTH_SHORT).show();}}
packagecom.example.radiobutton;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.widget.RadioButton;importandroid.widget.RadioGroup;importandroid.widget.TextView;importandroid.widget.Toast;importandroid.widget.RadioGroup.OnCheckedChangeListener;publicclassMainActivityextendsActivityimplementsOnCheckedChangeListener{privateRadioGrouprg;privateRadioButtonrb1,rb2;privateTextViewtv;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);rg=(RadioGroup)findViewById(R.id.rg);rb1=(RadioButton)findViewById(R.id.rb1);rb2=(RadioButton)findViewById(R.id.rb2);tv=(TextView)findViewById(R.id.tv);rg.setOnCheckedChangeListener(this);}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main,menu);returntrue;}@OverridepublicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){// TODO Auto-generated method stubif(group==rg){Stringrbstr=null;if(checkedId==rb1.getId()){rbstr=rb1.getText()+"选中";//tv.setText(rbstr);}elseif(checkedId==rb2.getId()){rbstr=rb2.getText()+"选中";//tv.setText(rbstr);}showToast(rbstr);}}publicvoidshowToast(Stringstr){Toast.makeText(this,str,Toast.LENGTH_SHORT).show();}}