Android UI开发之RadioButton

package com.android.liu.ui;

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

import com.android.liu.R;

public class RadioActivity extends Activity {
 RadioGroup m_RadioGroup;
 RadioButton m_radio1,m_radio2,m_radio3,m_radio4;
 @Override
 public void onCreate(Bundle inSaveState)
 {
  super.onCreate(inSaveState);
  setContentView(R.layout.radio);
  
  m_RadioGroup=(RadioGroup) this.findViewById(R.id.RadioGroup);
  m_radio1=(RadioButton) this.findViewById(R.id.RadioButton1);
  m_radio2=(RadioButton) this.findViewById(R.id.RadioButton2);
  m_radio3=(RadioButton) this.findViewById(R.id.RadioButton3);
  m_radio4=(RadioButton) this.findViewById(R.id.RadioButton4);
  
  m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(checkedId==m_radio2.getId())
    {
     display("正确答案:"+m_radio2.getText()+",恭喜你回答正确");
    }else{
     display("选择的是错误答案");
    }
    
   }
  });
  
 }
 
 public void display(String msg)
 {
  Toast.makeText(RadioActivity.this, msg, Toast.LENGTH_SHORT).show();
 }
}

<?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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="请选择正确答案" 
   
    ></TextView>
   
    <RadioGroup
    android:id="@+id/RadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px"
       >
       <RadioButton
        android:id="@+id/RadioButton1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/RadioButton1"
       />
      
        <RadioButton
        android:id="@+id/RadioButton2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/RadioButton2"
       />
      
        <RadioButton
        android:id="@+id/RadioButton3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/RadioButton3"
       />
      
        <RadioButton
        android:id="@+id/RadioButton4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/RadioButton4"
       />
       </RadioGroup>
      
       <RatingBar
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:numStars="3"
       android:id="@+id/ratingBar"
      
      
       ></RatingBar>
      
      
      
</LinearLayout>

你可能感兴趣的:(RadioButton)