Android中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
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类结构图
以上就是本文的全部内容,希望对大家的学习有所帮助。
- jquery的html,text,val
- Redis Cluster日常操作命令梳理
- MongoDB数据存储-深入了解
- Linux下通过受限bash创建指定权限的账号
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (上)
- MongoDB副本(一主一备+仲裁)环境部署记录
- 根据前后的子串求中间串的函数
- Centos6下zookeeper集群部署记录
- 抓取百度指数引发的图像数字识别
- 真正将标注文字遮盖的方法
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (中)
- li浮动时ul高度为0,解决ul自适应高度的几种方法
- 使用正则表达式求完整路径中的文件名
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (下)
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释