(初识android) android组件 RadioButton,CheckBox,Toast简单介绍
2012-03-08 17:31
573 查看
RadioButton单选框,CheakBox多选框,Toast翻译过来就是:吐司,烤面包片[U],在android肯定不是这个意思啦,它有点像是一个对话框Dialog,但是又不同,它会在显示后过一段时间自动消失,给用户已提示作用。
1、RadioButton,CheakBox在布局中:
从上我们可以看出,他们的大体使用和java并没有什么特别的变化,在使用RadioButton是必须是在一个RadioGroup中。
2.初始化
3.在相应的类中首先进行初始化和其他安卓控件一样,但在添加监听事件时有所不同,在RadioGroup添加监听是实现RadioGroup 的OnCheckedChangeListener事件,而相对应的CheckBox没有相对应的OnCheckedChangeListener,是使用CompoundButton的OnCheckedChangeListener事件。 给单选框添加监听事件时,是对RadioGrup进行监听,然后就、根据选择(CheckId和你的RadioButton)相匹配,在执行相应的操作。
4.Toast的使用,它并不需要在布局文件进行布局,直接实例化一个Toast直接可使用。 具体使用:
Toast具体说明:/article/5084332.html
1、RadioButton,CheakBox在布局中:
<RadioGroup android:id="@+id/rg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/manrb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/manrb" android:checked="true" /> <RadioButton android:id="@+id/womanrb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/womanrb" /> </RadioGroup> <CheckBox android:id="@+id/swimckb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/swimckb" android:layout_gravity="right" /> <CheckBox android:id="@+id/readckb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/readckb" /> <CheckBox android:id="@+id/runckb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/swimckb" />
从上我们可以看出,他们的大体使用和java并没有什么特别的变化,在使用RadioButton是必须是在一个RadioGroup中。
2.初始化
//初始化单选框 rg=(RadioGroup)findViewById(R.id.rg); manrb=(RadioButton)findViewById(R.id.manrb); womanrb=(RadioButton)findViewById(R.id.womanrb); //初始化多选框 swimckb=(CheckBox)findViewById(R.id.swimckb); readckb=(CheckBox)findViewById(R.id.readckb); runckb=(CheckBox)findViewById(R.id.runckb);
3.在相应的类中首先进行初始化和其他安卓控件一样,但在添加监听事件时有所不同,在RadioGroup添加监听是实现RadioGroup 的OnCheckedChangeListener事件,而相对应的CheckBox没有相对应的OnCheckedChangeListener,是使用CompoundButton的OnCheckedChangeListener事件。 给单选框添加监听事件时,是对RadioGrup进行监听,然后就、根据选择(CheckId和你的RadioButton)相匹配,在执行相应的操作。
//给单选框添加监听事件 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(manrb.getId()==checkedId) { toast=Toast.makeText(RadioButtonActivity.this,"您选择的是男", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 50,-100); toast.show(); } else if(womanrb.getId()==checkedId) { toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 50,-100); toast.show(); } } }); //给多选框添加监听 swimckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { //执行操作 } else{ //执行操作 } } }); readckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { //执行操作 } else{ //执行操作 } } }); runckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { //执行操作 } else{ //执行操作 } } });
4.Toast的使用,它并不需要在布局文件进行布局,直接实例化一个Toast直接可使用。 具体使用:
toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 50,-100); toast.show();
Toast具体说明:/article/5084332.html
相关文章推荐
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock 的简单介绍
- android组件介绍:textview、button、radiobutton、checkbox、chronometer、spinner、progressbar、seekbar等
- Android开发视频教程之十二(RadioGroup、RadioButton、CheckBox和Toast的使用)
- Android的四大基本组件的简单介绍
- android RadioButton和CheckBox组件的使用方法
- 【按住你的心】——Android开发CheckBox&RadioButton控件的简单使用
- [Android知识体系]之四大组件:ContentProvider简单介绍
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
- Android Design Support Library 中控件的使用简单介绍(一)介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android
- android四大组件基础介绍及打电话,发短信简单应用 &单元测试
- Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍
- 四,android四大组件基础介绍及打电话,发短信简单应用 &单元测试
- android中的RadioButton和CheckBox的简单用法
- Android开发--控件之RadioGroup、RadioButton、CheckBox、Toast
- Android简单组件之_ImageButton_CheckBox
- react-native-easy-toast, 一款简单易用的 Toast 组件,支持 Android&iOS.
- Android之RadioButton与RadioGroup,CheckBox,Switch,ToggleButton的介绍
- Android学习之四大组件简单介绍
- 一款简单易用的 Toast 组件,支持 Android&iOS
- 一款简单易用的 Toast 组件,支持 Android&iOS