国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中CheckBox復選框控件使用方法詳解

2019-10-22 18:30:36
字體:
來源:轉載
供稿:網友

CheckBox復選框控件使用方法,具體內容如下

一、簡介

1、

CheckBox,復選框,Android

2、類結構圖

CheckBox,復選框,Android

二、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、效果圖:

CheckBox,復選框,Android

2、代碼

fry.Activity01

package fry;import java.util.ArrayList;import java.util.List;import com.example.CheckBoxDemo1.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.LinearLayout;import android.widget.Toast;public class Activity01 extends Activity implements OnClickListener{  private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();  private LinearLayout ll_checkBoxList;  private Button btn_ok;//  CheckBox復選框控件使用方法//  這里是使用java代碼在LinearLayout里面添加控件//  1、新建LinearLayout布局//  2、建立CheckBox的XML的Layout文件//  3、通過View.inflate()方法創建CheckBox//  4、通過LinearLayout的addView方法添加CheckBox//  5、通過List<CheckBox>完成輸出功能  @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.activity01);    ll_checkBoxList=(LinearLayout) findViewById(R.id.ll_CheckBoxList);    btn_ok=(Button) findViewById(R.id.btn_ok);    String[] strArr={"你是學生嗎?","你是否喜歡android","您喜歡旅游嗎?","打算出國嗎?"};    for(String str:strArr){      CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);      checkBox.setText(str);      ll_checkBoxList.addView(checkBox);      checkBoxList.add(checkBox);    }    btn_ok.setOnClickListener(this);  }  @Override  public void onClick(View v) {    // TODO Auto-generated method stub    String str="";    for(CheckBox checkBox:checkBoxList){      if(checkBox.isChecked()){        str+=checkBox.getText().toString()+"/n";      }    }    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  }}

/CheckBoxDemo1/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <LinearLayout     android:id="@+id/ll_CheckBoxList"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical"    >          </LinearLayout>    <Button     android:id="@+id/btn_ok"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="確定"    />    </LinearLayout>

/CheckBoxDemo1/res/layout/checkbox.xml

<?xml version="1.0" encoding="utf-8"?><CheckBox xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" ></CheckBox>

四、收獲

 1、 View.inflate(this, R.layout.checkbox, null)方法里面的checkbox的XML

<?xml version="1.0" encoding="utf-8"?><CheckBox xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" ></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類結構圖

CheckBox,復選框,Android

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 社旗县| 汾西县| 介休市| 罗山县| 辽宁省| 雅安市| 宁德市| 平安县| 诸暨市| 鹤山市| 河曲县| 南和县| 汪清县| 商都县| 廊坊市| 苏尼特右旗| 仪陇县| 沁阳市| 叙永县| 黎川县| 青田县| 龙南县| 报价| 安宁市| 宁城县| 南开区| 渝北区| 罗甸县| 阿拉善盟| 金乡县| 南江县| 柯坪县| 铜梁县| 屯留县| 田阳县| 吉林市| 山东省| 罗定市| 壶关县| 湟源县| 延津县|