• CheckBox复选框控件


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

    一、建立工程,如图

    二、activity_main.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" >
    
        <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:text="确定"
            />
        
        
    
    </LinearLayout>
    View Code

    三、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="fill_parent"
        android:layout_height="wrap_content"
        >
        
        
    </CheckBox>
    View Code

    四、MainActivity.java中代码

    package com.study.checkbox;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity implements OnClickListener{
    
        private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            String[] checkboxText = new String[]{"你是学生吗?","是否喜欢android?","喜欢旅游吗?","打算出国吗?"};
            //动态加载布局
            LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
            //给指定的checkbox赋值
            for(int i=0; i<checkboxText.length;i++){
                //先获得checkbox.xml的对象
                CheckBox checkBox = (CheckBox)getLayoutInflater().inflate(R.layout.checkbox, null);
                checkBoxs.add(checkBox);
                checkBoxs.get(i).setText(checkboxText[i]);
                linearLayout.addView(checkBox, i);
                
            }
            setContentView(linearLayout);
            Button button = (Button)this.findViewById(R.id.button);
            button.setOnClickListener(this);
            
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
        @Override
        public void onClick(View arg0) {
            String s = "";
            for(CheckBox checkBox: checkBoxs){
                if(checkBox.isChecked()){
                    s += checkBox.getText() + "
    ";
                }
            }
            if("".equals(s)){
                s = "您还没有选中选项!";
            }
            
            new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();
            
        }
        
    }
    View Code

     五、效果图

  • 相关阅读:
    在awk里引用shell变量(支持正则)
    python模块pyautogui
    一个完整的搜索系统
    信息检索笔记(9)再论文档评分
    信息检索导论学习笔记(8)向量空间模型
    搜索引擎查询扩展
    信息检索笔记(10)Lucene文档评分机制
    Lucene的分析过程
    信息检索导论学习笔记(7)文档评分、词项权重计算
    信息检索导论学习笔记(5)
  • 原文地址:https://www.cnblogs.com/kingshow123/p/checkbox.html
Copyright © 2020-2023  润新知