CheckBox exercises
:
Compared with radioButton, the checkbox does not require a RadioGroup. Each checkbox is a separate control. It is easy to use isChecked to determine whether to select a CheckedChangeListener listener. For details, refer to the code.
Java code, which simplifies the setOnCheckedChangeListener listening code in each chekcbox.
Package zziss. android. checkboxtest;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. CheckBox;
Import android. widget. CompoundButton;
Import android. widget. Toast;
Public class CheckBoxTestActivity extends Activity {
/** Called when the activity is first created .*/
Private CheckBox cb_red;
Private CheckBox cb_blue;
Private CheckBox cb_gray;
Private CheckBox cb_green;
Private Button btn_chose;
Private CheckListener listener;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Cb_red = (CheckBox) this. findViewById (R. id. cb_red );
Cb_blue = (CheckBox) this. findViewById (R. id. cb_blue );
Cb_gray = (CheckBox) this. findViewById (R. id. cb_gray );
Cb_green = (CheckBox) this. findViewById (R. id. cb_green );
Btn_chose = (Button) this. findViewById (R. id. btn );
Listener = new CheckListener ();
Listener. iActivity = this;
Cb_red.setOnCheckedChangeListener (listener );
Cb_blue.setOnCheckedChangeListener (listener );
Cb_gray.setOnCheckedChangeListener (listener );
Cb_green.setOnCheckedChangeListener (listener );
Btn_chose.setOnClickListener (listener );
}
Public void showInfo (String str)
{
Toast toast = Toast. makeText (this, str, Toast. LENGTH_SHORT );
Toast. show ();
}
Public void showCheckTexts ()
{
String s = "";
S + = cb_red.isChecked ()? Cb_red.getText (). toString () + ";":"";
S + = cb_blue.isChecked ()? Cb_blue.getText (). toString () + ";":"";
S + = cb_gray.isChecked ()? Cb_gray.getText (). toString () + ";":"";
S + = cb_green.isChecked ()? Cb_green.getText (). toString () + ";":"";
ShowInfo (s );
}
}
Class CheckListener implements
Android. widget. CompoundButton. OnCheckedChangeListener,
Android. view. View. OnClickListener
{
@ Override
Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// TODO Auto-generated method stub
IActivity. showInfo (buttonView. getText (). toString ());
}
Public CheckBoxTestActivity iActivity;
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
IActivity. showCheckTexts ();
}
}
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 = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/info"/>
<CheckBox
Android: id = "@ + id/cb_red"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/cb_red"
/>
<CheckBox
Android: id = "@ + id/cb_blue"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/cb_blue"
/>
<CheckBox
Android: id = "@ + id/cb_gray"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/cb_gray"
/>
<CheckBox
Android: id = "@ + id/cb_green"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/cb_green"
/>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/btn_chose"
/>
</LinearLayout>
String. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "info"> checkbox exercise </string>
<String name = "app_name"> CheckBoxTest </string>
<String name = "cb_red"> Red </string>
<String name = "cb_blue"> blue </string>
<String name = "cb_gray"> gray </string>
<String name = "cb_green"> green </string>
<String name = "btn_chose"> View and select </string>
</Resources>