Android—RadioGroup和RadioButton的使用
2014-07-25 21:13
344 查看
在分享RadioGroup和RadioButton的使用方法之前,先顺带提到一个东西:Toast。Toast 是一个 View 视图,简单的说它的作用是在应用程序上浮动显示信息给用户,类似一个对话框Dialog,它永远不会获得焦点,不影响用户的输入等操作,主要用于 一些帮助 / 提示。Toast的用法很简单:Toast.makeText(context,
text, duration);三个参数分别为上下文菜单(一般写this,表示所在Activity),要显示的文本内容,显示时间长短(Toast.LENGTH_LONG,Toast.LENGTH_LONG,也可以直接写1或者0)。
比如当需要在点击一个Button按钮是提示用户输入信息时,只需一句 Toast.makeText(this, "请输入***", Toast.LENGTH_LONG).show();
好了,进入正题。首先还是布局文件,以事实说话才好。
总结:实现方法很简单,和普通Button的的点击时间差不多是一样的,简单Button实现的是OnClickListenner()接口,而RadioButton需要实现OnCheckedChangeListener()。
同时,对于有效合理地使用Toast对于我们程序代码的编写或者调试都会有帮助。
晚安。
text, duration);三个参数分别为上下文菜单(一般写this,表示所在Activity),要显示的文本内容,显示时间长短(Toast.LENGTH_LONG,Toast.LENGTH_LONG,也可以直接写1或者0)。
比如当需要在点击一个Button按钮是提示用户输入信息时,只需一句 Toast.makeText(this, "请输入***", Toast.LENGTH_LONG).show();
好了,进入正题。首先还是布局文件,以事实说话才好。
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请回答,今天你学习了吗?" /> <RadioGroup android:id="@+id/radiogroup" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学习了" /> <RadioButton android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没学习" /> </RadioGroup> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的回答:" /> </LinearLayout></span>然后就是java中实现:
<span style="font-size:12px;">public class MainActivity extends Activity { private RadioGroup radiogroup = null; private RadioButton yes = null; private RadioButton no = null; private TextView textview = null; // 定义组件类型 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 取得组件 radiogroup = (RadioGroup) findViewById(R.id.radiogroup); yes = (RadioButton) findViewById(R.id.yes); no = (RadioButton) findViewById(R.id.no); textview = (TextView) findViewById(R.id.textview); radiogroup.setOnCheckedChangeListener(new radioGroupCheckchange()); // 绑定事件监听器 } private class radioGroupCheckchange implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if (yes.isChecked()) { // 选中yes的RadioButton textview.setText("我的回答:" + yes.getText().toString()); Toast.makeText(MainActivity.this, "我今天学习了", Toast.LENGTH_LONG) .show(); // 用Toast显示信息 } else if (no.isChecked()) { //选中no的RadioButton textview.setText("我的回答:" + no.getText().toString()); Toast.makeText(MainActivity.this, "我今天没有学习", Toast.LENGTH_LONG) .show(); // 用Toast显示信息 } } } } </span>效果图:
总结:实现方法很简单,和普通Button的的点击时间差不多是一样的,简单Button实现的是OnClickListenner()接口,而RadioButton需要实现OnCheckedChangeListener()。
同时,对于有效合理地使用Toast对于我们程序代码的编写或者调试都会有帮助。
晚安。
相关文章推荐
- Android控件系列之RadioButton与RadioGroup使用方法
- Android RadioGroup和RadioButton使用
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
- Android开发之RadioGroup与RadioButton控件使用
- Android基础总结八:单选框RadioGroup,RadioButton的使用
- Android控件笔记——使用RadioGroup和RadioButton实现单选效果
- Android自学笔记之RadioGroup和RadioButton的使用
- Android控件系列之RadioButton与RadioGroup使用方法 详细出处参考:http://www.jb51.net/article/31799.htm
- Android之单选控件RadioGroup,RadioButton使用
- Android开发视频教程之十二(RadioGroup、RadioButton、CheckBox和Toast的使用)
- Android使用RadioGroup和RadioButton实现筛选条件功能总结好坑啊
- Android 使用RadioGroup和RadioButton实现单选效果
- android RadioGroup和RadioButton使用注意
- Android--CheckBox复选框、RadioGroup和RadioButton及其使用
- Android RadioGroup及RadioButton的使用方法
- android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进)
- Android控件系列之RadioButton与RadioGroup的基本使用
- Android UI控件之RadioGroup、RadioButton
- Activity常用控件二——RadioGroup、RadioButton、CheckBox的使用
- Android:UI控件RatingBar、SeekBar、ProgressBar、RadioGroup、RadioButton、CheckBox、TextView