雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton

本文介绍了Android中的CheckBox和RadioButton控件。CheckBox是多选按钮,有选中和未选中两种状态;RadioButton属于单选按钮,一个RadioGroup内只能选中一个。文中详细阐述了RadioButton与RadioGroup的关系,并对比了RadioButton与CheckBox的区别,包括它们的UI表示及选中行为。此外,还提供了Android应用开发中添加和处理这两个控件的基本步骤。

CheckBox是Android提供的多选按钮控件,它只有true和false两种状态。在xml中默认为false(未选中),也可以这样设置

android:checked="true"

  这样的话,就默认选中了。


RadioButton为单选按钮。一个RadioGroup可以包含多个RadioButton。

RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置




RadioButton和CheckBox的区别:
1、单个RadioButton在选中后,通过点击无法变为未选中
    单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个
     一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示
     CheckBox在大部分UI框架中默认都以矩形表示


下面看代码:

开发步骤: 
1,新建一个Android应用程序 
2,在布局文件中创建两个TextView控件标签,一个RadioGroup控件和四个CheckBox控件,并为其设置属性和值 
3,在Activity中,声明所有控件变量并根据id获得控件对象 ,设置RadioButton和CheckBox的监听器,
4,当选中all按钮的时候,要求所有的多选按钮都被选中

package com.tangbc.choosedemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
	private RadioGroup radioGroup;
	private RadioButton boyButton;
	private RadioButton girlButton;
	private CheckBox wowCB;
	private CheckBox lolCB;
	private CheckBox dotaCB;
	private CheckBox allCB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
        //为所有控件绑定id
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		boyButton = (RadioButton) findViewById(R.id.boyButton);
		girlButton = (RadioButton) findViewById(R.id.girlButton);

		wowCB = (CheckBox) findViewById(R.id.wowCheckBox);
		lolCB = (CheckBox) findViewById(R.id.lolCheckBox);
		dotaCB = (CheckBox) findViewById(R.id.dotaCheckBox);
		allCB = (CheckBox) findViewById(R.id.allCheckBox);
		
         //绑定监听器
		radioGroup.setOnCheckedChangeListener(new RadioGroupListener());
		wowCB.setOnCheckedChangeListener(new CheckBoxListener());
		lolCB.setOnCheckedChangeListener(new CheckBoxListener());
		dotaCB.setOnCheckedChangeListener(new CheckBoxListener());
		allCB.setOnCheckedChangeListener(new CheckBoxListener());
	}

	// 导入android.widget.CompoundButton.OnCheckedChangeListener
	class CheckBoxListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (buttonView.getId() == wowCB.getId()) {
				if (isChecked == true) {
					//获取点击按钮的text文本
					String text = buttonView.getText().toString();
					System.out.println(text );
				} else {
					System.out.println("cancelWOW");
				}
			} else if (buttonView.getId() == lolCB.getId()) {
				if (isChecked == true) {
					String text = buttonView.getText().toString();
					System.out.println(text );
				} else {
					System.out.println("cancelLOL");
				}
			} else if (buttonView.getId() == dotaCB.getId()) {
				if (isChecked == true) {
					String text = buttonView.getText().toString();
					System.out.println(text );
				} else {
					System.out.println("cancelDota");
				}
			} else if (buttonView.getId() == allCB.getId()) {
				if (isChecked == true) {        //all按钮为选中状态后,其他的按钮都被选中
					System.out.println("all");
					wowCB.setChecked(true);
					lolCB.setChecked(true);
					dotaCB.setChecked(true);
				} else {
					String text = buttonView.getText().toString();
					System.out.println(text );
				}
			} else {
				System.out.println("default");
			}
		}

	}

	// 导入android.widget.RadioGroup.OnCheckedChangeListener
	class RadioGroupListener implements
			android.widget.RadioGroup.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if (checkedId == boyButton.getId()) {
				System.out.println("boy");
				Toast.makeText(MainActivity.this, "你选的是boy", Toast.LENGTH_SHORT).show();
			} else if (checkedId == girlButton.getId()) {
				System.out.println("girl");
				Toast.makeText(MainActivity.this, "你选的是girl", Toast.LENGTH_SHORT).show();
			}
		}

	}

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

}

XML文件


<RelativeLayout 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: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" >

    <TextView
        android:id="@+id/sexTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你是男生还是女生" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/sexTextView" >

        <RadioButton
            android:id="@+id/boyButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="boy" />

        <RadioButton
            android:id="@+id/girlButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="girl" />
    </RadioGroup>

    <TextView
        android:id="@+id/gameTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/radioGroup"
        android:text="你喜欢玩的游戏" />

    <CheckBox
        android:id="@+id/wowCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/gameTextView"
        android:checked="false"
        android:text="WOW" />

    <CheckBox
        android:id="@+id/lolCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/wowCheckBox"
         android:checked="false"
        android:text="LOL" />

    <CheckBox
        android:id="@+id/dotaCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lolCheckBox"
        android:text="Dota" />

    <CheckBox
        android:id="@+id/allCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dotaCheckBox"
        android:text="all" />

</RelativeLayout>

点我下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值