CheckBox的一些用法(内容包含动态加载布局)
2015-08-10 19:46
141 查看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="选择"/>
</LinearLayout>
//checkbox的xml文件
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</CheckBox>
public class CheckboxsActivity extends Activity implements OnClickListener {
private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 动态加载布局
LinearLayout linearlayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
String[] ss = new String[]
4000
{ "你好吗?", "你喜欢android吗?", "你要去哪?", "还有呢?" };
// 动态加载checkbox并给赋值
for (int i = 0; i < ss.length; i++) {
CheckBox checkbox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);
checkBoxs.add(checkbox);
checkBoxs.get(i).setText(ss[i]);
// 把checkbox添加到linearlayout中
linearlayout.addView(checkbox, i);
}
setContentView(linearlayout);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String s = "";
for (CheckBox checkbox : checkBoxs) {
if (checkbox.isChecked()) {
s += checkbox.getText() + "\n";
}
}
if ("".equals(s)) {
s = "你没有选择任何选项";
}
new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();
}
}
@1.有个小问题是如果AlertDialog在for循环代码中去了会出现弹出窗口要关闭很多次才能关,也就是说循环了几次就要关几次。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="选择"/>
</LinearLayout>
//checkbox的xml文件
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</CheckBox>
public class CheckboxsActivity extends Activity implements OnClickListener {
private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 动态加载布局
LinearLayout linearlayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
String[] ss = new String[]
4000
{ "你好吗?", "你喜欢android吗?", "你要去哪?", "还有呢?" };
// 动态加载checkbox并给赋值
for (int i = 0; i < ss.length; i++) {
CheckBox checkbox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);
checkBoxs.add(checkbox);
checkBoxs.get(i).setText(ss[i]);
// 把checkbox添加到linearlayout中
linearlayout.addView(checkbox, i);
}
setContentView(linearlayout);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String s = "";
for (CheckBox checkbox : checkBoxs) {
if (checkbox.isChecked()) {
s += checkbox.getText() + "\n";
}
}
if ("".equals(s)) {
s = "你没有选择任何选项";
}
new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();
}
}
@1.有个小问题是如果AlertDialog在for循环代码中去了会出现弹出窗口要关闭很多次才能关,也就是说循环了几次就要关几次。
相关文章推荐
- CSS笔记
- poj2362 Square
- CSU 1511 残缺的棋盘
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- 535 Error: authentication failed
- 数据结构(15)静态链表
- A+B
- 一道面试题细说C++类型转换
- PHP中间uniqid在高并发重复问题
- 基于 Android NDK 的学习之旅-----HelloWorld
- 算法1:k-近邻
- java基础第十讲——数据库、单例设计模式、服务器、项目(数据库—服务器—网络)
- NOIP2009 Hankson的趣味题
- 【COCI 2013】Inspeaker
- 数据库
- JDBC应用小案例
- 创建UIWebView
- 数据结构(14)单链表
- MySQL数据库与登录注册
- 防止.NET应用程序内存溢出(OOM)的一些措施