`

android toast 和checkbox and radiogroup的使用

 
阅读更多

1. main.xml 布局文件

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
    <RadioGroup 
        android:id="@+id/groupId"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RadioButton 
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"
            android:checked="true"
            
            />
        <RadioButton 
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female"
            />
        
    </RadioGroup>
    
    <TextView 
        android:id="@+id/love"
        android:text="@string/love"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />
    
    <CheckBox 
        android:id="@+id/swinning"
        android:text="@string/swinning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>
    
    <CheckBox 
        android:id="@+id/running"
        android:text="@string/running"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>
    
    <CheckBox 
        android:id="@+id/reading"
        android:text="@string/reading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>

</LinearLayout>

 

2. Java 调用

 

 

package com.helloworld;

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

/**
 * 常控件的使用
 * @author liuqing
 * @version 1.0
 *
 */
public class ViewCompoentActivity extends Activity {
	
	private RadioGroup radioGroup;
	
	private CheckBox running;
	
	private CheckBox reading;
	
	private CheckBox swinning;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.radioGroup = (RadioGroup)this.findViewById(R.id.groupId);
        this.reading = (CheckBox)this.findViewById(R.id.reading);
        this.swinning = (CheckBox)this.findViewById(R.id.swinning);
        this.running = (CheckBox)this.findViewById(R.id.running);
        //对readioGroup 添加监听器
        this.radioGroup.setOnCheckedChangeListener(
        		new OnCheckedChangeListener(){
            
        	//这里是返加的ID值
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.female) {
					Toast.makeText(getApplication(), "female",
							Toast.LENGTH_SHORT).show();
				}
				else if (checkedId == R.id.male) {
					Toast.makeText(getApplication(), "male", 
							Toast.LENGTH_SHORT).show();
				}
			}
        	
        }
       );
        this.running.setOnCheckedChangeListener 
              (new CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					//如果被选中isChecked 值为true
					if (isChecked) {
						System.out.println("running is true");
						Toast.makeText(getApplication(), 
								"running is true", Toast.LENGTH_SHORT).show();
					}
					else {
						System.out.println("running is false");
					}
					
				}
        	
             }
            );
        
        this.swinning.setOnCheckedChangeListener(
        		new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							System.out.println("swinning is true");
							Toast.makeText(getApplication(), "swinning is true", 
									Toast.LENGTH_SHORT).show();
						}
						else {
							System.out.println("swinning is false");
						}
						
					}
        	
                  }
        		);
        
        this.reading.setOnCheckedChangeListener(
        		new CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (isChecked) {
						System.out.println("reading is true");
						Toast.makeText(getApplication(), "reading is true",
								Toast.LENGTH_SHORT).show();
					}
					else {
						System.out.println("reading is false");
					}
					
				}
        	
               }
        	);
        
    }
}
分享到:
评论

相关推荐

    Radiogroup、Checkbox、Toast

    NULL 博文链接:https://conkeyn.iteye.com/blog/1581051

    android RadioButton和CheckBox组件的使用方法

    本次实验中主要是学习如何使用RadioGroup,CheckBox,RadioButton和Toast这几个控件,android UI开发中也会经常用到他们

    Android编程入门很简单.(清华出版.王勇).part1

    5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12使用下拉列表框——Spinner 5.2.13实例——请选择工作年限 5.2.14实例——动态修改Spinner项 5.2.15 使用进度条——ProgressBar 5.2.16实例——...

    Android入门到精通源代码.

    2.3 Android SDK的下载和安装 2.3.1 下载Android SDK 2.3.2 安装Android SDK 2.3.3 创建Android虚拟设备 2.4 Eclipse的下载和安装 2.4.1 下载和安装Eclipse 2.4.2 安装和配置Eclipse中Android插件 2.5 使用Eclipse...

    Android编程入门很简单.(清华出版.王勇).part2

    5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12使用下拉列表框——Spinner 5.2.13实例——请选择工作年限 5.2.14实例——动态修改Spinner项 5.2.15 使用进度条——ProgressBar 5.2.16实例——...

    Android上机实验:身高计算器的实现.pdf

    import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.Toast; private EditText et1; private EditText et2; private CheckBox cb; private...

    Android开发案例驱动教程 配套代码

    3.3 使用Android SDK帮助 23 3.3.1 Android SDK API文档 23 3.3.2 Android SDK开发指南 24 3.3.3 Android SDK samples 24 3.4 使用DDMS帮助调试程序 26 3.4.1 启动DDMS 26 3.4.2 Device 28 3.4.3 Emulator ...

    android开发入门与实战(下)

    7.8 温馨的提醒——Toast和Notification应用 7.8.1 实例操作演示 7.8.2 实例编程实现 7.9 本章小结 第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences...

    Android开发应用实战详解源代码

    2.1.1 各种库和android运行环境 2.1.2 应用程序框架 2.1.3 操作系统层 2.1.4 应用程序 2.2 android应用程序组成 2.2.1 activity 2.2.2 intent和intent filter 2.2.3 service介绍 2.2.4 broadcastintentreceiver ...

    android开发入门与实战(上)

    7.8 温馨的提醒——Toast和Notification应用 7.8.1 实例操作演示 7.8.2 实例编程实现 7.9 本章小结 第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences...

    《Google Android开发入门与实战》.pdf

    7.8 温馨的提醒——toast和notification应用 127 7.8.1 实例操作演示 128 7.8.2 实例编程实现 129 7.9 本章小结 135 第8章 移动信息仓库——android的数据存储操作 136 8.1 android数据存储概述 ...

    实验3-Android-应用的界面开发(2).doc

    实验目的 1、掌握常用界面组件TextView、EditText、Button、ImageButton、RadioGroup CheckBox、ImageView、AutoCompleteTextView、Spinner 2、掌握Toast的用法 2. 实验步骤 1. 实现"radionbutton"的功能 参考代码...

    Google.Android开发入门与实战

    7.8 温馨的提醒——Toast和Notification应用 7.8.1 实例操作演示 7.8.2 实例编程实现 7.9 本章小结 第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——RadioGroup组与onCheckedChanged事件 4.7 专业相框设计——...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——RadioGroup组与onCheckedChanged事件 4.7 专业相框设计——...

    Android基础知识详解

    Dalvik和Android系统 11 Dalvik虚拟机的主要特征 12 Android应用开发和Dalvik虚拟机 15 Activity生命周期 16 一、Activity栈 16 二、Activity的4种状态 16 三、Activity的生命周期 17 四、实例说明 18 Android控件的...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——RadioGroup组与onCheckedChanged事件 4.7 专业相框设计——...

    Google Android开发入门与实战的代码

    7.8 温馨的提醒——Toast和Notification应用 127 7.8.1 实例操作演示 128 7.8.2 实例编程实现 129 7.9 本章小结 135 第8章 移动信息仓库——Android的数据存储操作 136 8.1 Android数据存储概述 ...

    集成目前Android主流优秀第三方组件

    追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格ProgressDialog 自定义样式WebView 圆形进度条(RoundProgressBar)、垂直进度条...

Global site tag (gtag.js) - Google Analytics