Android控件之-RadioGroup和RadioButton

单项选择的组件一般情况是有RadioGroup和RadiaButton组成的

package com.ko8e;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Activity01 extends Activity {
    /** Called when the activity is first created. */
	private TextView textView = null;
	private RadioButton button1 = null;
	private RadioButton button2 = null;
	private RadioButton button3 = null;
	private RadioButton button4 = null;
	private RadioGroup group = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.button1);
        button1 = (RadioButton) findViewById(R.id.button1);
        button2 = (RadioButton) findViewById(R.id.button2);
        button3 = (RadioButton) findViewById(R.id.button3);
        button4 = (RadioButton) findViewById(R.id.button4);
        group = (RadioGroup) findViewById(R.id.group);

       group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId == button3.getId()) {
					DisplayToast("回答正确!" + button3.getText().toString());
				} else {
					DisplayToast("回答错误,请重新选择");
				}
			}
		});
    }
    public void DisplayToast(String str) {
    	Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
    	toast.setGravity(Gravity.TOP, 0, 200);
    	toast.show();
    }
}

 

layout.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"
    >
<TextView  
	android:id="@+id/view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/question"
    />
<RadioGroup
	android:id="@+id/group"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:orientation="vertical"
	android:layout_x="3px"
	android:layout_y="54px"
	/>
<RadioButton
	android:id="@+id/button1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/button1"
	/>
	<RadioButton
	android:id="@+id/button2"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/button2"
	/>
	<RadioButton
	android:id="@+id/button3"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/button3"
	/>
	<RadioButton
	android:id="@+id/button4"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/button4"
	/>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值