CheckBox默认的情况下是未选中的状态,如果想修改这个默认值的话,可以将<checkbox>中的android:checked设置为true或者使用CheckBox.setChecked方法设置都可以实现复选的功能。
实战案例一:

复选框控件使用,实现当用户去点击确定按钮的时候能弹出用户所选择的这个选项

布局文件:main.xml

<?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:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        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/checkbox"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    </CheckBox>

程序主要代码:

public class CheckBoxDemo extends Activity implements OnClickListener {    private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 注释掉 setContentView,也就是说在Oncreate加载xml的时候我们使用动态加载布局的方式        // setContentView(R.layout.main);        String[] checkBoxTest = new String[] {                "你是学生吗?", "是否喜欢Android?", "你喜欢旅游吗?", "打算出国吗?"        };                /*         * 查看android api的Activity 中的 getLayoutInflater()方法 :public LayoutInflater getLayoutInflater ()         * 返回的是 LayoutInflater 这个就是Android中的动态加载布局         * 查看 LayoutInflater,它表示会加载一个布局xml文件到相应的视图对象中,它主要使用inflate()方法来实现的         * public View inflate (int resource, ViewGroup root)         * 它会从指定的xml资源中加载一个新的视图布局,并且填充它的父视图,如果没有父视图的话,root参数设置为null。         */                LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);        //给指定的checkbox赋值        for(int i = 0; i < checkBoxTest.length; i++) {            //先获得checkBox.xml的对象            CheckBox checkBoxLayout = (CheckBox)getLayoutInflater().inflate(R.layout.checkbox, null);            checkBoxs.add(checkBoxLayout);            checkBoxs.get(i).setText(checkBoxTest[i]);                        //实现了在main主布局中,通过LinearLayout在for循环中添加checkbox。            linearLayout.addView(checkBoxLayout, i);                       setContentView(linearLayout);                        Button button = (Button)findViewById(R.id.button);            button.setOnClickListener(this);        }    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        //当用户点击按钮的时候,要取出这个布局        String str = "";        for(CheckBox checkBox : checkBoxs){            if(checkBox.isChecked()){                str += checkBox.getText() + "\n";            }        }        if("".equals(str)){            str = "你还没有选中选项!!";        }        //使用一个提示框来显示用户信息        new AlertDialog.Builder(this).setMessage(str).setPositiveButton("关闭", null).show();    }}

程序Demo执行结果:





更多相关文章

  1. Android(安卓)DrawerLayout 高仿QQ5.2双向侧滑菜单
  2. Kotlin&Anko, 扔掉XML开发Android应用
  3. 【android】应用架构一一一一一Activity和Fragment的对比分析
  4. Android常用适配器分析(如何制作简易Launcher)
  5. Android(安卓)性能优化方案大全
  6. android屏幕适配建议 (二)
  7. Android(安卓)- 布局(layout) 详解
  8. Android(安卓)4编程入门经典pdf
  9. android学习——GridView实现主界面布局

随机推荐

  1. android毛玻璃背景简单实现
  2. Android(安卓)拉起另一个APP
  3. Android修改状态栏颜色全方位教程
  4. Android(安卓)Studio ——在不root手机的
  5. 设置ubuntu Android(安卓)sdk环境变量
  6. Android之intent传值的三种方法
  7. Android(安卓)PopupWindow响应返回键最正
  8. android 蓝牙 获取蓝牙地址名字
  9. Android代码调试报错
  10. cordova打包app环境搭建