android勾选控件_Android中CheckBox复选框控件使用方法详解

本文详细介绍了如何在Android中使用CheckBox控件,包括在LinearLayout中动态添加CheckBox、设置文本、监听选中状态,并提供了完整的代码示例。
摘要由CSDN通过智能技术生成

CheckBox复选框控件使用方法,具体内容如下

一、简介

1、

69925dfe7aaae716dccfbe79ee039010.png

2、类结构图

180c9b4e4845122c687bbf4a644ccd7e.png

二、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完成输出功能

for(CheckBox checkBox:checkBoxList)

三、代码实例

1、效果图:

f1ff06cc00e7bc35259d486374b1d765.png

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 checkBoxList=new ArrayList();

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完成输出功能

@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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/ll_CheckBoxList"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

android:id="@+id/btn_ok"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="确定"

/>

/CheckBoxDemo1/res/layout/checkbox.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

四、收获

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

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的创建

private List checkBoxList=new ArrayList();

4、for(CheckBox checkBox:checkBoxList)

遍历

5、list类结构图

0353e2a241cbe26fe074b7455d96fdf3.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值