安卓编程:复选框(CheckBox)的简单使用方法案例

来源:互联网 发布:詹姆斯生涯场均数据 编辑:程序博客网 时间:2024/05/18 00:21

CheckBox和Button一样,是一种古老的控件,它的优点在于,不用用户去填写具体的信息,只需轻轻点击。

CheckBox的应用范围也很广泛,比如常见的爱好选择。

简单的CheckBox的应用案例:

1.调用setOnCheckedChangeListener()方法,并把CompoundButton.OnCheckedChangeListener实例作为参数传入

2.在CompoundButton.OnCheckedChangeListener的onCheckedChanged()方法里,取得被选中Android复选框的实例


效果图:




页面布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="example.administrator.com.test1222application.MainActivity">    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="同意"        android:id="@+id/tongyi"        />    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#000"/>    <LinearLayout        android:layout_width="match_parent"        android:id="@+id/Linear"        android:layout_height="wrap_content">        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="AA"            android:id="@+id/aa"            />        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="BB"            android:id="@+id/bb"            />        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="CC"            android:id="@+id/cc"            />        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="DD"            android:id="@+id/dd"            />    </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btn_sub"        android:text="提交"        /></LinearLayout>

java代码:

package example.administrator.com.test1222application;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private CheckBox ckOK;    private LinearLayout Linear;    private Button btn_sub;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ckOK= (CheckBox) findViewById(R.id.tongyi);        Linear= (LinearLayout) findViewById(R.id.Linear);        btn_sub= (Button) findViewById(R.id.btn_sub);        ckOK.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if(isChecked){                    ckOK.setText("已同意");                    /*ckOK.setEnabled(false);*/                }else{                    ckOK.setText("同意");                }            }        });        btn_sub.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                StringBuffer sb=new StringBuffer();                //拿到所有的子类长度                int cNum = Linear.getChildCount();                for (int i = 0; i < cNum; i++) {                    //根据i 拿到每一个CheckBox                    CheckBox cb= (CheckBox) Linear.getChildAt(i);                    //判断CheckBox是否被选中                    if(cb.isChecked()){                        //把被选中的文字添加到StringBuffer中                        sb.append(cb.getText().toString());                    }                }                Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();            }        });    }}

当点击“同意”时,监听到选中事件,改变CheckBox的文本。

当选中一些CheckBox后,点击“提交”按钮,Toast出被选中按钮的所有文本

运行效果:

选中“同意”



点击“提交”




注意:

CheckBox有一个属性可以让他被选中后就不可以改变选项了

setEnabled(false)属性,当他为false时,CheckBox就不可以被改变了
运行效果:





阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 取快递收费怎么办 淘宝手机号错误怎么办 开店注册营业执照怎么办 不支持农村淘宝怎么办 微博抽中现金了怎么办 汽车气罐拖审 怎么办 摩拜退不了押金怎么办 酒店房门打不开怎么办 玉林市危化证怎么办 微信被标记诈骗怎么办 出现问题怎么办 解决不了问题怎么办 网上银行被盗怎么办 被告联系不到怎么办 被骗去传销怎么办 租房遇到传销怎么办 无人机丢失怎么办 朋友异地传销怎么办 广州天天下雨怎么办 福睿斯油耗高怎么办 图片看不清楚怎么办 农产品配送执照怎么办 st烯碳退市股票怎么办 白鞋太简单了怎么办 歧视残疾人怎么办 有鼻炎唱歌怎么办 鼻炎唱歌难听怎么办 家养乌龟下蛋怎么办 孕妇肚胀怎么办 孕妇肚子阵痛怎么办 怀孕40天怎么办 受风嘴歪了怎么办 孕妇脸部被吹歪怎么办 头发比较硬怎么办 超能战队玩着卡怎么办 陶泥干了怎么办 脸上有色素怎么办 马克笔擦不掉怎么办 唱歌有鼻音怎么办 人生面临选择怎么办 银饰品氧化发黑怎么办