您的位置:首页 > 移动开发 > Android开发

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();

好了,进入正题。首先还是布局文件,以事实说话才好。

<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对于我们程序代码的编写或者调试都会有帮助。

晚安。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: