01-12:RadioGroup、RadioButton、CheckBox、Toast的用法
来源:互联网 发布:java mvc模式是什么 编辑:程序博客网 时间:2024/06/17 01:12
RadioGroup:一个单选按钮的组
RadioButton:一个单选按钮
CheckBox:一个多选按钮(多选按钮不分组)
Toast:效果为:当编辑了短信,但是又没发送时,弹出一小框框提示你“信息已保存”。
/****一个Activity对象***/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布局文件:
<?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>
效果如下:
0 0
- 01-12:RadioGroup、RadioButton、CheckBox、Toast的用法
- RadioButton和CheckBox的区别,RadioButton&RadioGroup
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- (转)android常用控件(二) RadioGroup RadioButton CheckBox Toast的使用
- 第八节--控件(一)RadioGroup,RadioButton,CheckBox,Toast
- Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
- Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】
- Android开发--控件之RadioGroup、RadioButton、CheckBox、Toast
- 控件RadioGroup、RadioButton和CheckBox的使用
- RadioGroup,RadioButton CheckBox控件的使用
- RadioGroup RadioButton ,checkBox ,menu
- RadioButton RadioGroup CheckBox
- 【幻化万千戏红尘】qianfeng-Android-Day03-RadioButton及RadioGroup的用法、CheckBox、ProgressBar基础学习:
- Toast, CheckBox, RadioButton
- android RadioGroup和RadioButton的用法
- CheckBox和RadioButton以及RadioGroup
- android:RadioGroup RadioButton CheckBox备忘
- Android控件RadioButton&RadioGroup&CheckBox
- 凤凰股份很多很多分公司风格是
- poj1961-kmp的next数组的简单应用(2)
- Android开发之如何获取wifi的名称即SSID
- 2014微软编程之美初赛第一场第三题 活动中心
- 设计模式之适配器模式(Adapter)
- 01-12:RadioGroup、RadioButton、CheckBox、Toast的用法
- IOS系列——蓝牙(中心)2
- 研究 UIActivityViewController
- Floyd算法
- java右下角弹窗效果
- 【20140422】paxos的一次总结
- eclipse 切换svn账号
- mysql大小写问题
- hive HQL笔记