Android初级教程小案例之单选框RadioGroup与复选框CheckBox

Android里面的单选框和html中的其实是一样的效果。这里用到两个控件:CheckBox和RadioGroup。直接上代码:

radio.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<RadioGroup
	android:id="@+id/genderGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <RadioButton
    	android:id="@+id/femaleButton"
 	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:text="@string/female"
  	    />
    <RadioButton
    	android:id="@+id/maleButton"
 	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:text="@string/male"
  	    />
</RadioGroup>
<CheckBox
	android:id="@+id/swim"
 	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:text="@string/swim"
  	/>
<CheckBox
	android:id="@+id/run"
 	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:text="@string/run"
  	/>
<CheckBox
	android:id="@+id/read"
 	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:text="@string/read"
  	/>
</LinearLayout>

String.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Activity07!</string>
    <string name="app_name">activity07</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="swim">swim</string>
    <string name="run">run</string>
    <string name="read">read</string>
</resources>

RadioTest:

package mars.activity07;

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

public class RadioTest extends Activity {
    /** Called when the activity is first created. */
	//对控件对象进行声明
	private RadioGroup genderGroup = null;
	private RadioButton femaleButton = null;
	private RadioButton maleButton = null;
	private CheckBox swimBox = null;
	private CheckBox runBox = null;
	private CheckBox readBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);
        //通过控件的ID来得到代表控件的对象
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
					Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show();
				}
			}
		});

        //为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)//未选中到选中状态是执行这里:
				{
					System.out.println("swim is checked");
				}
				else//由选中状态到未选中状态时候执行这里:
				{
					System.out.println("swim is unchecked");
				}
			}
		});
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("run is checked");
				}
				else
				{
					System.out.println("run is unchecked");
				}
			}
		});
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("read is checked");
				}
				else
				{
					System.out.println("read is unchecked");
				}
			}
		});
    }

}

注册文件进行注册:

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".RadioTest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

执行效果图:

时间: 2024-08-04 10:16:25

Android初级教程小案例之单选框RadioGroup与复选框CheckBox的相关文章

3.Android之单选按钮RadioGroup和复选框Checkbox学习

单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

组合框里添加复选框的方法(使用勾选的假象,用图片代替而已,并非QT原生支持)

组合框可以看作是列表框和文本框的组合,因其占据的空间少,使用操作方便,常被界面设计人员用于界面开发设计中,在有限个输入的条件下,组合框常用来代替文本框,这样从用户使用角度来看,更趋人性化,所见即所得.然好的控件永远敢不上应用的步伐,有时常规控件并不能满足应用的需要,经常需要在现有的控件上做扩展.有些应用需要在组合框的列表框的每一项前加复选框,以便可以控制列表框每一项的状态(选中还是未选中),显然现有的组合框(列表框+文本框的组合)不能满足应用要求.那么怎么得到一个超强组合框(列表框+文本框+复选

android完成注册页面的下拉框及单复选框(1)

package com.example.dell.mylogin; import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.Che

用jquery修改默认的单选框radio或者复选框checkbox选择框样式

默认的radio和checkbox选框很难看.我去看了一下qq注册的页面.发现单选和复选框并没有用<input>,居然是用是A标签.然后用css背景图片展示选择框,用JavaScript控制切换.然后我自己用jqeury写了一个这样功能的. 先是html代码 性别 <div id="box-6"> <label>性别</label> <a href="#" name="male" id=&qu

可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1,准备工作:VS2015 (15对WPF的支持变得异常的好,调试模式下允许自动更改属性.),随VS发布的Blend,几个基础类: 1 public class RelayCommand : ICommand 2 { 3 #region Fields 4 5 readonly Action<object

单选按钮RadioGroup与复选框CheckBox

在AndroidApp应用中,单选按钮和复选框也是经常使用的,下面我们一起学习一下.我们需要学习Android中的基本控件:(1)单选按钮RadioGroup.(2)复选框CheckBox. 一.设计登录窗口 打开"res/layout/activity_main.xml"文件.  1.分别从工具栏向activity拖出1个单选按钮列表RadioGroup(注意自动包含3个单选按钮RadioButton).2个复选框CheckBox.1个按钮Button.这3个控件均来自Form Wi

JS中获取页面单选框radio和复选框checkbox中当前选中的值

单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意</td>  <td><input type="radio" name="radioid">基本满意</td> var radio=document.getElementsByName("radio"); va

checbox复选框实现radio单选框的单选功能

checbox复选框实现radio单选框的单选功能:大家知道复选框可以一次选中多个,单选按钮每次只能够选中其中的一个,但是单选按钮比较霸道,你选中以后,只能够且必须选中其中一个,所有下面就通过checkbox复选框模拟实现单选按钮的功能,但是能够取消选中的项.代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author"

【mfc】组框、单选框控件与复选框控件

VC6中的MFC中的单选框与复选框控件也不简单,它没有VS中C#那样可以之间通过一个封装之后的函数进行判断,该单选框/复选框有没有被选中啊?或者控制单选框/复选框的选中状态.这东西比较复杂,通过相应的值来控制的. 一.基本目标 下面就用一个简单例子来说明VC6中的MFC中的单选框与复选框控件,点击"确定"按钮能够判断对话框中的所有单选框与复选框的选中状态.Radio1-Radio3互成一个组,组内仅能够有一个组员被选中,Radio4-Radio5也互成一组,选择Radio4能够令复选框