CheckBox複選框控制項,checkbox複選框
CheckBox複選框控制項
一、簡介
1、
2、類結構圖
二、CheckBox複選框控制項使用方法
這裡是使用java代碼在LinearLayout裡面添加控制項
1、建立LinearLayout布局
2、建立CheckBox的XML的Layout檔案
3、通過View.inflate()方法建立CheckBox
CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);
4、通過LinearLayout的addView方法添加CheckBox
ll_checkBoxList.addView(checkBox);
5、通過List<CheckBox>完成輸出功能
for(CheckBox checkBox:checkBoxList)
三、代碼執行個體
1、:
2、代碼
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 Activity01 extends Activity implements OnClickListener{18 private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();19 private LinearLayout ll_checkBoxList;20 private Button btn_ok;21 // CheckBox複選框控制項使用方法22 // 這裡是使用java代碼在LinearLayout裡面添加控制項23 // 1、建立LinearLayout布局24 // 2、建立CheckBox的XML的Layout檔案25 // 3、通過View.inflate()方法建立CheckBox26 // 4、通過LinearLayout的addView方法添加CheckBox27 // 5、通過List<CheckBox>完成輸出功能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={"你是學生嗎?","你是否喜歡android","您喜歡旅遊嗎?","打算出國嗎?"};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="確定"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>
四、收穫
1、 View.inflate(this, R.layout.checkbox, null)方法裡面的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 </CheckBox>
2、用代碼在LinearLayout中添加CheckBox方法
1)通過View.inflate()方法建立CheckBox
CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);
2)通過LinearLayout的addView方法添加CheckBox
ll_checkBoxList.addView(checkBox);
3、List<CheckBox>的建立
private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();
4、for(CheckBox checkBox:checkBoxList)
遍曆
5、list類結構圖