The CheckBox check button is a special type of button with two statuses, which can be selected or not selected. You can now define the multiple-choice button in the layout file, and then perform event monitoring setOnCheckedChangeListener on each multiple-choice button. The isChecked is used to determine whether the option is selected.
Main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/theme"/>
<CheckBox
Android: id = "@ + id/apple"
Android: text = "@ string/apple"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<CheckBox
Android: id = "@ + id/banana"
Android: text = "@ string/banana"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
RadioGroupActivity. java
Package Android. Activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. CheckBox;
Import android. widget. CompoundButton;
Import android. widget. Toast;
Public class RadioGroupActivity extends Activity {
/** Called when the activity is first created .*/
Private CheckBox appleCheck = null;
Private CheckBox bananaCheck = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the object representing the control through the control ID
AppleCheck = (CheckBox) findViewById (R. id. apple );
BananaCheck = (CheckBox) findViewById (R. id. banana );
// Set the listener for the RadioGroup. Note that the listener here is different from the listener for the Button control.
AppleCheck. setOnCheckedChangeListener (new CompoundButton. OnCheckedChangeListener (){
Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// TODO Auto-generated method stub
If (isChecked ){
Toast. makeText (RadioGroupActivity. this, "Congratulations, you have a lot of apple left.", Toast. LENGTH_LONG). show ();
}
}
}); Www.2cto.com
BananaCheck. setOnCheckedChangeListener (new CompoundButton. OnCheckedChangeListener (){
Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// TODO Auto-generated method stub
If (isChecked ){
Toast. makeText (RadioGroupActivity. this, "Congratulations, you have a lot of bananas left.", Toast. LENGTH_LONG). show ();
}
}
});
}
}
From sunset hut