Android—RadioGroup和RadioButton的使用

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

晚安。再见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值