CheckBox check box control, checkbox check box
CheckBox check box Control
I. Introduction
1,
2. Class Structure Diagram
Ii. How to Use the CheckBox check box Control
Java code is used to add controls in LinearLayout.
1. New LinearLayout Layout
2. Create the XML Layout file of the CheckBox.
3. Create a CheckBox using the View. inflate () method
CheckBox checkBox = (CheckBox) View. inflate (this, R. layout. checkbox, null );
4. Add the CheckBox using the addView method of LinearLayout
Ll_checkBoxList.addView (checkBox );
5. Use List <CheckBox> to complete the output function.
For (CheckBox checkBox: checkBoxList)
Iii. code example
1 ,:
2. Code
Fry. Activity01
1 package fry; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 import com. example. checkBoxDemo1.R; 7 8 import android. app. activity; 9 import android. OS. bundle; 10 import android. view. view; 11 import android. view. view. onClickListener; 12 import android. widget. button; 13 import android. widget. checkBox; 14 import android. widget. linearLayout; 15 import android. widget. toast; 16 17 public class Act Ivity01 extends Activity implements OnClickListener {18 private List <CheckBox> checkBoxList = new ArrayList <CheckBox> (); 19 private LinearLayout ll_checkBoxList; 20 private Button btn_ OK; 21 // CheckBox check box control usage method 22 // here is to add control 23 // 1 in LinearLayout using java code, create a LinearLayout Layout 24 // 2, and create a CheckBox XML Layout file 25 // 3, through View. use the inflate () method to create CheckBox26 // 4. Use the addView method of LinearLayout to add CheckBox27 // 5, and use the List <CheckBox> method. Output Function 28 @ Override29 protected void onCreate (Bundle savedInstanceState) {30 // TODO Auto-generated method stub31 super. onCreate (savedInstanceState); 32 setContentView (R. layout. activity01); 33 ll_checkBoxList = (LinearLayout) findViewById (R. id. ll_CheckBoxList); 34 btn_ OK = (Button) findViewById (R. id. btn_ OK); 35 String [] strArr = {"Are you a student? "," Do you like android "," Do you like traveling? "," Are you planning to go abroad? "}; 36 for (String str: strArr) {37 CheckBox checkBox = (CheckBox) View. inflate (this, R. layout. checkbox, null); 38 checkBox. setText (str); 39 ll_checkBoxList.addView (checkBox); 40 checkBoxList. add (checkBox); 41} 42 btn_ OK .setOnClickListener (this); 43} 44 @ Override45 public void onClick (View v) {46 // TODO Auto-generated method stub47 String str = ""; 48 for (CheckBox checkBox: checkBoxList) {49 if (checkBox. isChecked () {50 str + = checkBox. getText (). toString () + "\ n"; 51} 52} 53 Toast. makeText (this, str, Toast. LENGTH_SHORT ). show (); 54} 55}
/CheckBoxDemo1/res/layout/activity01.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <LinearLayout 8 android: id = "@ + id/ll_CheckBoxList" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: orientation = "vertical" 12> 13 14 15 </LinearLayout> 16 17 <Button 18 android: id = "@ + id/btn_ OK" 19 android: layout_width = "match_parent" 20 android: layout_height = "wrap_content" 21 android: text = "OK" 22/> 23 24 25 </LinearLayout>
/CheckBoxDemo1/res/layout/checkbox. xml
1 <?xml version="1.0" encoding="utf-8"?>2 <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="match_parent"4 android:layout_height="match_parent"5 android:orientation="vertical" >6 7 8 </CheckBox>
Iv. Gains
1. the checkbox XML in the View. inflate (this, R. layout. checkbox, null) Method
1 <?xml version="1.0" encoding="utf-8"?>2 <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="match_parent"4 android:layout_height="match_parent"5 android:orientation="vertical" >6 </CheckBox>
2. Use code to add the CheckBox method to LinearLayout.
1) create a CheckBox using the View. inflate () method
CheckBox checkBox = (CheckBox) View. inflate (this, R. layout. checkbox, null );
2) Add the CheckBox using the addView method of LinearLayout
Ll_checkBoxList.addView (checkBox );
3. Create List <CheckBox>
Private List <CheckBox> checkBoxList = new ArrayList <CheckBox> ();
4. for (CheckBox checkBox: checkBoxList)
Traversal
5. list class structure