Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习

  1. CheckBox

    CheckBox,复选按钮。扩展自CompoundButton类。具有选中和非选中两种状态。复选按钮在单个使用时与状态按钮功能相近。与状态按钮不同在于复选按钮可以多个同时使用。当使用多个复选按钮时,可实现多选功能。其主要xml属性是继承自CompoundButton的android:checked属性,该属性用于设置复选按钮的状态。其主要方法是isChecked(),该方法用于获取复选按钮的当前状态。
    ·复选按钮

    • 继承自CompoundButton
    • 两个主要用途:
      做状态按钮使用
      做复选框使用
      ·主要属性:
    • Android:checked
      ·主要方法:
    • isChecked()
      ·主要事件
    • setOnCheckedChangeListener
      (OnCheckedChangeListener l)
  2. RadioButton和RadioGroup
    RadioButton,单选按钮。扩展自CompoundButton类。单选按钮控件通常与RadioGroup搭配使用。RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组。同一按钮组内的单选按钮只能有一个被选中。RadioGroup最主要的xml属性是android:checkedButton,该属性用于设置组内默认被选中的单选按钮的id。RadioGroup最主要的方法有getCheckedRadioButtonId()和check(int radioButtonId)。

    RaidoButton
    ·单选按钮

    • 继承自CompoudButton
    • 需要与RadioGroup搭配使用
      ·主要属性:
    • android:checked
      ·主要方法:
    • isChecked()

    RaidoGroup
    ·单选按钮组

    • 继承自Linearyout
    • 需要设置组内单选按钮的排列方向
      ·主要属性:
    • android:checkedButton
      ·主要方法:
    • getCheckedRadioButtonId()
    • check(int rdoId)
  3. 实例

这里写图片描述

这里写图片描述

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <CheckBox 
       android:id="@+id/chbAgree"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="同意以上协议"
       android:checked="false"/>
    <LinearLayout 
        android:id="@+id/llroot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone">
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="姓名"/>
            <EditText 
                android:id="@+id/etName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:hint="请输入姓名"
                android:inputType="text"/>
        </LinearLayout>
            <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:id="@+id/tvAge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="年龄"/>
            <EditText 
                android:id="@+id/etAge"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:hint="请输入年龄"
                android:inputType="number"/>
        </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="性别"/>
            <RadioGroup 
                android:id="@+id/rgSex"
                android:layout_width="0dp"
                android:layout_weight="1.0"
                android:layout_height="wrap_content"
                android:checkedButton="@+id/rbMan"
                android:orientation="horizontal"
                >
            <RadioButton 
                android:id="@+id/rbMan"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="男"
                />
            <RadioButton 
                android:id="@+id/rbMadal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="女"/>
            </RadioGroup>
        </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="爱好"
                android:layout_gravity="center_vertical"/>
        <CheckBox 
            android:id="@+id/chbread"
            android:layout_width="0dp"
            android:layout_weight="1.0"
            android:layout_height="wrap_content"
            android:text="阅读"/>
        <CheckBox 
            android:id="@+id/chbCode"
            android:layout_width="0dp"
            android:layout_weight="1.0"
            android:layout_height="wrap_content"
            android:text="敲代码"/>
         </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    <Button 
        android:id="@+id/btnLogin"
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="wrap_content"
        android:text="注册"
        android:onClick="doClick"/>
    <Button 
        android:id="@+id/btnReset"
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="wrap_content"
        android:text="重置"
        android:onClick="doClick"/>
    </LinearLayout>
    </LinearLayout>
</LinearLayout>

Activity:

package com.example.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private
    CheckBox chbAgree,chbRead,chbCood;
    private RadioGroup rgSex;
    private EditText etName,etAge;
    private LinearLayout llRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rgSex = (RadioGroup) findViewById(R.id.rgSex);
        chbAgree = (CheckBox) findViewById(R.id.chbAgree);
        chbRead = (CheckBox) findViewById(R.id.chbread);
        chbCood = (CheckBox) findViewById(R.id.chbCode);
        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
        llRoot = (LinearLayout) findViewById(R.id.llroot);
        chbAgree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            /**
             * buttonView 事件源对象
             * isChecked 最新状态(是否被选中)
             */
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    llRoot.setVisibility(View.VISIBLE);
                }else{
                    llRoot.setVisibility(View.GONE);
                }

            }
        });
    }
    public void doClick(View v){
        switch (v.getId()) {
        case R.id.btnLogin:
            StringBuilder sb = new StringBuilder("注册信息:\n");
            sb.append(etName.getText().toString()).append('\n');
            sb.append(rgSex.getCheckedRadioButtonId() == R.id.rbMan ? "男":"女").append('\n');
            sb.append(etAge.getText().toString()).append('\n');
            if(chbRead.isChecked()){
                sb.append(chbRead.getText().toString()).append('\t');
            }
            if(chbCood.isChecked()){
                sb.append(chbCood.getText().toString()).append('\t');
            }
            Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
            break;

        case R.id.btnReset:
            etName.getText().clear();
            etAge.getText().clear();
            //etAge.setText("");
            chbRead.setChecked(false);
            chbCood.setChecked(false);
            rgSex.check(R.id.rbMan);
            break;
        }
    }

}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值