android应用开发笔记---常用控件RadioGroup和RadioButton及Toast
来源:互联网 发布:国家电网软件开发 编辑:程序博客网 时间:2024/06/06 00:41
这三个控件分别是单选,多选,还有提示框,效果如下
相关代码
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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(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");}}}); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("swim is checked");}else{System.out.println("swim is unchecked");}}}); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("run is checked");}else{System.out.println("run is unchecked");}}}); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("read is checked");}else{System.out.println("read is unchecked");}}}); } }
布局文件
<?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" ><TextViewandroid:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><RadioGroupandroid: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><CheckBoxandroid:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /><CheckBoxandroid:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /><CheckBoxandroid:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /></LinearLayout>
- android应用开发笔记---常用控件RadioGroup和RadioButton及Toast
- Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】
- Android开发--控件之RadioGroup、RadioButton、CheckBox、Toast
- Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
- Android开发之四(五):常用控件之单项选择(RadioGroup和RadioButton)
- (转)android常用控件(二) RadioGroup RadioButton CheckBox Toast的使用
- Android 应用开发笔记 - 单项选择(RadioGroup, RadioButton)
- android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进)
- 2.常用控件:RadioGroup和RadioButton
- Android控件RadioButton&RadioGroup
- Android控件——RadioButton和RadioGroup
- Android基本控件RadioGroup和RadioButton学习
- Android按钮控件之RadioGroup和RadioButton
- Android控件之RadioGroup和RadioButton
- Android控件之RadioButton和RadioGroup
- android 手机开发笔记--RadioGroup RadioButton
- Android RadioGroup和RadioButton案例及详解
- Android RadioGroup和RadioButton案例及详解
- 理解Bash shell中shift命令
- C#调用c++的标准动态链接库dll
- 使用VS2010 C#编写ActiveX控件
- c#调用c++写成的dll文件
- java synchronized详解
- android应用开发笔记---常用控件RadioGroup和RadioButton及Toast
- 删除control list 记录
- 【白话系列】倍增算法
- 二元树中和为某一值的所有路径[数据结构]
- 静态成员函数
- bat批处理
- VMware Workstation 9下基于Ubuntu 12.10服务器版本的Hadoop集群的配置
- Android权限之二packages.xml解析
- 重走java路