当前位置: 移动技术网 > IT编程>移动开发>Android > Android中CheckBox复选框控件使用方法详解

Android中CheckBox复选框控件使用方法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

夏日乐悠悠下载,猪价格走势,27888.tv

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类结构图

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网