`
ko8e
  • 浏览: 50503 次
  • 性别: Icon_minigender_1
  • 来自: 龙岩
社区版块
存档分类
最新评论

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>
分享到:
评论
1 楼 416849838 2011-12-05  
.................

相关推荐

Global site tag (gtag.js) - Google Analytics