Android控件之checkbox

本文介绍了Android中的CheckBox控件,包括其简介和特点。通过3个具体例子,展示了CheckBox在用户信息获取中的应用,如性别选择和多选爱好等。在最后一个案例中,详细给出了实现特定界面和功能的代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、checkbox

CheckBox简介:
CheckBox和Button一样,是与用户点击有关的控件,也是一种古老的控件,它的优点在于,不用用户去填写具体的信息,只需轻轻点击,缺点在于只有“是”和“否”两种情况,但我们往往利用它的这个特性,来获取用户的一些信息。

2举例分析

《1》

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView//一个关于爱好的选择,往往是多选
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择爱好"/>
    <CheckBox//用checkbox给出两个爱好
        android:id="@+id/checkbox_LOL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOL"/>
    <CheckBox
        android:id="@+id/checkbox_study"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学习"/>

</LinearLayout>

这里写图片描述
《2》
checkbox放在密码后面,作为可视按钮

    <EditText
        android:id="@+id/checkbox_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:password="true"/>
    <CheckBox
        android:id="@+id/checkbox_showpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码可见"
        android:layout_gravity="right"//调整位置到右边
        android:layout_marginRight="30dp"/>//距离右边界30dp

这里写图片描述
《3》综合运用
搭建如下界面:
这里写图片描述
要求:
1、性别默认为男性
2、爱好选择可以多个
3、密码开始时加密,点击密码可见后显示密码
4、点击确定按钮,在控制台上打印出性别和爱好

代码如下:

//这个是搭建界面,整体为线性布局,从上到下依次为:LinearLayout (用于选择性别)》》LinearLayout (用于选择爱好)》》button(提交数据)》》EditText(输入密码)》》Checkbox(密码可见)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择性别"
        android:textSize="30dp"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radiogroup"
        android:checkedButton="@id/radio"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio"
            android:text="男"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其它"/>
    </RadioGroup>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择爱好"/>
    <CheckBox
        android:id="@+id/checkbox_LOL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOL"/>
    <CheckBox
        android:id="@+id/checkbox_study"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学习"/>

</LinearLayout>
    <Button
        android:id="@+id/button_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:text="确定"/>

    <EditText
        android:id="@+id/checkbox_password"//输入密码id
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:password="true"/>
    <CheckBox
        android:id="@+id/checkbox_showpassword"//显示密码id
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码可见"
        android:layout_gravity="right"
        android:layout_marginRight="30dp"/>
</LinearLayout>

在activity.java中的部分代码:

setContentView(R.layout.radiobutton);
        mCheckboxLOL = (CheckBox) findViewById(R.id.checkbox_LOL);//初始化
        mCheckboxstudy = (CheckBox) findViewById(R.id.checkbox_study);
        mPassword = (EditText) findViewById(R.id.checkbox_password);
        mCheckboxShowPassword = (CheckBox) findViewById(R.id.checkbox_showpassword);
   mCheckboxShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//注意这里是对checkbox建立的选择事件  
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mPassword.setTransformationMethod(null);//当选择了密码可见时,显示密码,注意这里的id是输入密码的id
                } else {
                    mPassword.setTransformationMethod(new PasswordTransformationMethod());
                }

            }
        });
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mButtonSelect = (Button) findViewById(R.id.button_select);//对提交数据按钮建立点击事件
        mButtonSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedId = mRadioGroup.getCheckedRadioButtonId();
                mRadioButton = (RadioButton) findViewById(checkedId);
                Log.d("sex", "您的性别是:" + mRadioButton.getText());
       //下面是打印出爱好,用ArrayList        
                ArrayList<String> hobby = new ArrayList<String>();
                if (mCheckboxLOL.isChecked()) {
                    hobby.add("LOL");
                }
                if (mCheckboxstudy.isChecked()) {
                    hobby.add("学习");
                }
                for (String ho : hobby) {
                    Log.d("hobby", "我的爱好" + ho);
                }
            }
        });

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值