CheckBox複選框控制項,checkbox複選框

來源:互聯網
上載者:User

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類結構圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.