Android --- 20 --- CheckBox check box, androidcheckbox
MainActivity. java:
Import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. checkBox; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private CheckBox basketball, football, baseball, pingpang; private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); basketball = (CheckBox) findViewById (R. id. basketball); baseball = (CheckBox) findViewById (R. id. baseball); football = (CheckBox) findViewById (R. id. football); pingpang = (CheckBox) findViewById (R. id. pingpang); button = (Button) findViewById (R. id. button); button. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString s = ""; boolean flag = false; if (basketball. isChecked () {s + = basketball. getText (). toString (); flag = true;} if (baseball. isChecked () {s + = baseball. getText (). toString (); flag = true;} if (football. isChecked () {s + = football. getText (). toString (); flag = true;} if (pingpang. isChecked () {s + = pingpang. getText (). toString (); flag = true;} if (flag = false) {Toast. makeText (MainActivity. this, "no options", 0 ). show ();} else {Toast. makeText (MainActivity. this, s, 0 ). show ();}}}
Activity_main.xml:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. example. checkboxdemo2.MainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" "/> <LinearLayout android: layout_width =" fill_parent "android: layout_height = "wrap_content" android: orientation = "vertical"> <CheckBox android: id = "@ + id/basketball" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "basketball"/> <CheckBox android: id = "@ + id/football" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = ""/> <CheckBox android: id = "@ + id/baseball" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = ""/> <CheckBox android: id = "@ + id/pingpang" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout> <Button android: id = "@ + id/button" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "OK"/> </LinearLayout>