Android控件之checkbox

来源:互联网 发布:怎样复制淘宝链接 编辑:程序博客网 时间:2024/05/17 01:04

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);                }            }        });

这里写图片描述

0 0
原创粉丝点击