CheckBox复选框控件使用方法,具体内容如下一、简介类结构图二、CheckBox复选框控件使用方法这里是使用java代码在LinearLayout里面添加控件新建LinearLayout布局建立CheckBox的XML的Layout文件
CheckBox复选框控件使用方法,具体内容如下
一、简介
类结构图
二、CheckBox复选框控件使用方法
这里是使用java代码在LinearLayout里面添加控件
新建LinearLayout布局
建立CheckBox的XML的Layout文件
通过View.inflate()方法创建CheckBox
CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);
通过LinearLayout的addView方法添加CheckBox
ll_checkBoxList.addView(checkBox);
通过List<CheckBox>完成输出功能
for(CheckBox checkBox:checkBoxList)
三、代码实例
效果图:
代码
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(); }}
--结束END--
本文标题: Android中CheckBox复选框控件使用方法详解
本文链接: https://www.lsjlt.com/news/221768.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0