`
Bauble
  • 浏览: 66967 次
  • 性别: Icon_minigender_1
  • 来自: Mercury
社区版块
存档分类
最新评论

Android11_Radio和CheckBox

阅读更多

一、RadioGroup和RadioButton的使用方法

RadioGroup和RadioButton代表的是Android中单选按钮的一种控件。对于单选按钮来说,每次只能选一个,如果有多组需要单选的信息,如:男、女,匿名发表、实名发表等,此时则需要组来区分哪几个单选按钮时一组的。

main.xml

 

<RadioGroup 
    	android:id="@+id/radioGroup"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:orientation="vertical"
    >
    	<RadioButton 
    		android:id="@+id/male"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content" 
    		android:text="@string/male"
    	/>
    	<RadioButton 
    		android:id="@+id/female"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content" 
    		android:text="@string/female"
    	/>
</RadioGroup>

 

  CtrlActivity.java

 

radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
male = (RadioButton)findViewById(R.id.male);
female = (RadioButton)findViewById(R.id.female);
//给radioGroup添加监听器
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if(female.getId() == checkedId){
			System.out.println("famale");
			//使用toast提示信息
			Toast.makeText(CtrlActivity.this, "您选择了女!", Toast.LENGTH_SHORT).show();
		}
		else if(male.getId() == checkedId){
			System.out.println("male");
			Toast.makeText(CtrlActivity.this, "您选择了男!", Toast.LENGTH_SHORT).show();
		}
	}
});
 

二、CheckBox的使用方法

main.xml

 

<CheckBox 
    	android:id="@+id/football"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:text="@string/football"
/>

 CtrlActivity.java

 

football = (CheckBox)findViewById(R.id.football);
//为多选按钮添加监听器
football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			System.out.println("football is checked");
			Toast.makeText(CtrlActivity.this, "您选择了足球!", Toast.LENGTH_SHORT).show();
		}
		else{
			System.out.println("football is unchecked");
			Toast.makeText(CtrlActivity.this, "您取消了足球选择!", Toast.LENGTH_SHORT).show();
		}
	}
});
 

三、Toast的基本用法

用于设置提示信息,用起来很方便。只需要一行代码。

 

Toast.makeText(CtrlActivity.this, "选择成功!", Toast.LENGTH_SHORT).show();

  其中makeText方法有三个参数,分别为当前Activity对象,输出数据以及Toast的长度。

 

四、完整代码

main.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:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/app_name"
    />
    <TextView 
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="@string/radioText"
    />
    <RadioGroup 
    	android:id="@+id/radioGroup"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:orientation="vertical"
    >
    	<RadioButton 
    		android:id="@+id/male"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content" 
    		android:text="@string/male"
    	/>
    	<RadioButton 
    		android:id="@+id/female"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content" 
    		android:text="@string/female"
    	/>
    </RadioGroup>
    <CheckBox 
    	android:id="@+id/football"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:text="@string/football"
    />
    <CheckBox 
    	android:id="@+id/basketball"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:text="@string/basketball"
    />
    <CheckBox 
    	android:id="@+id/volleyball"
    	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content" 
    	android:text="@string/volleyball"
    />
    
</LinearLayout>

  strings.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, CtrlActivity!</string>
    <string name="app_name">测试选择控件</string>
    <string name="radioText">下面是单选框控件</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="football">足球</string>
    <string name="basketball">篮球</string>
    <string name="volleyball">排球</string>
</resources>

  CtrlActivity.java

 

package com.android.activity;

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 CtrlActivity extends Activity {
	
	private RadioGroup radioGroup = null;
	private RadioButton male = null;
	private RadioButton female = null;
	
	private CheckBox football = null;
	private CheckBox basketball = null;
	private CheckBox volleyball = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        male = (RadioButton)findViewById(R.id.male);
        female = (RadioButton)findViewById(R.id.female);
        
        football = (CheckBox)findViewById(R.id.football);
        basketball = (CheckBox)findViewById(R.id.basketball);
        volleyball = (CheckBox)findViewById(R.id.volleyball);
        
        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if(female.getId() == checkedId){
				System.out.println("famale");
				//使用toast提示信息
				Toast.makeText(CtrlActivity.this, "您选择了女!", Toast.LENGTH_SHORT).show();
			}
			else if(male.getId() == checkedId){
				System.out.println("male");
				Toast.makeText(CtrlActivity.this, "您选择了男!", Toast.LENGTH_SHORT).show();
			}
		}
	});
        
        //为多选按钮添加监听器
        football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				System.out.println("football is checked");
				Toast.makeText(CtrlActivity.this, "您选择了足球!", Toast.LENGTH_SHORT).show();
			}
			else{
				System.out.println("football is unchecked");
				Toast.makeText(CtrlActivity.this, "取消足球选择!", Toast.LENGTH_SHORT).show();
			}
		}
	});
        
        basketball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				System.out.println("basketball is checked");
				Toast.makeText(CtrlActivity.this, "您选择了篮球!", Toast.LENGTH_SHORT).show();
			}
			else{
				System.out.println("basketball is unchecked");
				Toast.makeText(CtrlActivity.this, "取消篮球选择!", Toast.LENGTH_SHORT).show();
			}
		}
	});
        
        volleyball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				System.out.println("volleyball is checked");
				Toast.makeText(CtrlActivity.this, "您选择了排球!", Toast.LENGTH_SHORT).show();
			}
			else{
				System.out.println("volleyball is unchecked");
				Toast.makeText(CtrlActivity.this, "取消排球选择!", Toast.LENGTH_SHORT).show();
			}
		}
	});
    }
}

 运行结果:


     

  • 大小: 31.7 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    在Android中使用Radio和Checkbox的源码例子.rar

    在Android开发中,Radio和Checkbox是两种常用的UI组件,它们分别用于实现单选和多选功能。本示例源码提供了在Android应用中如何有效利用这两种控件的实践案例。 RadioGroup与RadioButton RadioGroup是Android中管理...

    check_and_radio_box.rar_Radio

    在实现复选框功能时,开发者会使用各种编程语言的库和框架,如JavaScript的jQuery UI,Python的Tkinter,或Android的CheckBox类。 接着,单选按钮(Radio Button),又称为选项按钮,主要用于提供一组互斥的选择。...

    Android开发之Radio和Checkbox用法举例

    摘要:Java源码,Android,Android源码 Android开发之Radio和Checkbox用法举例,Radio和Checkbox在Android开发中也相当普遍,几乎大部分程序都需要用到这些基本的窗体组件,Android开发中的Radio和Checkbox究竟如何...

    Android checkbox 实现单选

    总结,实现Android中的“Checkbox单选”功能,实际上我们使用的是RadioGroup和RadioButton的组合。通过RadioGroup来管理RadioButton,确保每次只有一个RadioButton被选中,同时设置监听器来响应用户的单选操作。这种...

    RadioAndCheck_android源码_

    在Android开发中,`RadioGroup`和`CheckBox`是两种常用的UI组件,它们分别用于实现单选和多选功能。本文将深入解析Android源码,探讨`RadioGroup`与`CheckBox`的工作原理及其在实际应用中的实现方式。 首先,我们来...

    Android 开灯 关灯 checkbox Radio组件用法演示.rar

    Android 开灯 关灯 checkbox Radio组件用法演示,向大家展示了如何为CheckBox和RadioButton添加监听器及开关灯业务代码,通过单击不同的按钮,切换开灯图片和关灯图片,设置程序操作状态如下:  public void ...

    checkBox的单选多选

    本教程将详细介绍如何在Android中实现CheckBox的单选和多选,并简要介绍迭代器的使用方法。 首先,让我们来理解一下“单选”和“多选”的概念。单选指的是用户只能从一组选项中选择一个,而多选则允许用户选择多个...

    019_UI_常用组件-BasicViews-button-checkbox-radio

    在Android UI设计中,BasicViews是一组基础的视图组件,包括按钮(Button)、复选框(Checkbox)和单选按钮(Radio Button),这些都是构建用户界面不可或缺的部分。本教程“019_UI_常用组件-BasicViews-button-...

    checkbox,RaidoButton,改变默认的图标

    对于JavaScript和React.js,你可以利用CSS来改变`input[type="checkbox"]`和`input[type="radio"]`的伪元素,例如`::before`和`::after`,或者使用第三方库提供的API来定制样式。 总的来说,改变`Checkbox`和`...

    自定义RadioButton&CheckBox样式的实现

    在Android开发中,RadioButton和CheckBox是两种常用的UI控件,用于用户进行单选或多选操作。自定义这些控件的样式可以提升应用的界面美观度和用户体验。本篇将详细介绍如何实现自定义RadioButton和CheckBox样式的...

    Android CheckBox中设置padding无效解决办法

    接着,我们可以在CheckBox的XML布局中设置`android:button`属性,将它指向我们刚创建的`radio_style.xml`文件,这样CheckBox就会根据其状态显示相应的图片: ```xml &lt;CheckBox android:id="@+id/my_checkbox" ...

    Android控件大全以及各布局控件的使用方式

    android:id="@+id/radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option A" android:checked="true"/&gt; ``` #### 三、高级控件介绍 除了基本控件...

    android自带的icons图标汇集

    6. 复选框图标:checkbox_off_background、checkbox_on_background 这两个图标用于表示复选框的状态,off 表示未选中,on 表示选中。 7. 对话框图标:dark_header、dialog_frame 这两个图标用于表示对话框的样式,...

    【android5.0】自定义带动画的RadioPreference

    android5.0后,google对checkbox,switch,radio做了动画效果,包括这些类衍生的Preferen控件:SwitchPreferenc,CheckBoxPreference 但这其中并没有RadioPreference控件。假如你需要RadioPreference或者一个带特殊功能...

    android控件示例

    综上所述,了解并熟练使用`Checkbox`和`RadioButton`对于开发功能丰富的Android应用至关重要。它们提供了直观的用户交互方式,使用户能够轻松地进行选择操作。通过深入学习和实践,开发者可以创造出更加友好和实用的...

    checkbox单选多选

    Checkbox,通常称为复选框,允许用户在多个选项中进行选择,可以同时选择一个或多个选项,这与单选按钮(Radio Button)不同,单选按钮在同一时间只能选择一个选项。在本主题中,我们将深入探讨Checkbox的单选和多选...

    Android常见Views速查表

    android:id="@+id/yes_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/yes" android:textAppearance="?android:textAppearanceMedium"/&gt; ...

    Android-Widgets-Buttons

    在本篇中,我们详细介绍了Android中的`Button`及其相关控件,包括`ImageButton`、`RadioButton`、`CheckBox`和`ToggleButton`。这些控件是构建用户界面的基础组件,对于提高应用程序的交互性和可用性至关重要。...

    Android程序源码--单选、多选按钮

    在Android开发中,单选按钮(RadioButton)和多选按钮(CheckBox)是用户界面中常见的交互元素,用于收集用户的选择信息。本资源包含的是关于如何在Android应用中实现这两种控件的源码示例。 首先,单选按钮...

    自定义RadioButton

    一个RadioButton通常由一个TextView和一个CheckBox组成,其中TextView用于显示文字,CheckBox则用于显示选中状态。在默认情况下,CheckBox的图标是系统预设的。要自定义这个图标,我们可以使用`android:drawableTop`...

Global site tag (gtag.js) - Google Analytics