android 单选框实现

 

Radio.java

 

 

[java]  view plain copy
  1. package archie.android.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.widget.RadioButton;  
  7. import android.widget.RadioGroup;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. public class Radio extends Activity {  
  12.     /** 
  13.      * 创建TextView对象 
  14.      * 创建RadioGroup对象 
  15.      * 创建4个RadioButton对象 
  16.      */  
  17.     TextView        m_TextView;  
  18.     RadioGroup      m_RadioGroup;  
  19.     RadioButton     m_Radio1, m_Radio2, m_Radio3, m_Radio4;  
  20.   
  21.   
  22.     /** Called when the activity is first created. */  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState)  
  25.     {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.   
  29.         /** 
  30.          * 获得TextView对象 
  31.          * 获得RadioGroup对象 
  32.          * 获得4个RadioButton对象 
  33.          */  
  34.         m_TextView = (TextView) findViewById(R.id.TextView01);  
  35.         m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);  
  36.         m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);  
  37.         m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);  
  38.         m_Radio3 = (RadioButton) findViewById(R.id.RadioButton3);  
  39.         m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);  
  40.   
  41.         /* 设置事件监听  */  
  42.         m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  43.             @Override  
  44.             public void onCheckedChanged(RadioGroup group, int checkedId)  
  45.             {  
  46.                 // TODO Auto-generated method stub  
  47.                 if (checkedId == m_Radio2.getId())  
  48.                 {  
  49.                     DisplayToast("正确答案:" + m_Radio2.getText() + ",恭喜你,回答正确!");  
  50.                 }  
  51.                 else  
  52.                 {  
  53.                     DisplayToast("请注意,回答错误!");  
  54.                 }  
  55.             }  
  56.         });  
  57.     }  
  58.       
  59.     /* 显示Toast  */  
  60.     public void DisplayToast(String str)  
  61.     {  
  62.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);  
  63.         //设置toast显示的位置  
  64.         toast.setGravity(Gravity.TOP, 0220);  
  65.         //显示该Toast  
  66.         toast.show();  
  67.     }  
  68. }  

 

main.xml

 

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.     android:id="@+id/TextView01"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello"  
  12.     />  
  13.   <RadioGroup  
  14.     android:id="@+id/RadioGroup01"  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.     android:orientation="vertical"  
  18.     android:layout_x="3px"  
  19.     android:layout_y="54px"   
  20.     >  
  21.     <RadioButton  
  22.       android:id="@+id/RadioButton1"  
  23.       android:layout_width="wrap_content"  
  24.       android:layout_height="wrap_content"  
  25.       android:text="@string/RadioButton1"  
  26.     />  
  27.     <RadioButton  
  28.       android:id="@+id/RadioButton2"  
  29.       android:layout_width="wrap_content"  
  30.       android:layout_height="wrap_content"  
  31.       android:text="@string/RadioButton2"  
  32.     />  
  33.     <RadioButton  
  34.       android:id="@+id/RadioButton3"  
  35.       android:layout_width="wrap_content"  
  36.       android:layout_height="wrap_content"  
  37.       android:text="@string/RadioButton3"  
  38.     />  
  39.     <RadioButton  
  40.       android:id="@+id/RadioButton4"  
  41.       android:layout_width="wrap_content"  
  42.       android:layout_height="wrap_content"  
  43.       android:text="@string/RadioButton4"  
  44.     />  
  45.     </RadioGroup>      
  46. </LinearLayout>  

 

String.xml

 

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Android底层是基于什么操作系统?</string>  
  4.     <string name="app_name">RadioButton</string>  
  5.     <string name="RadioButton1">Windows</string>  
  6.     <string name="RadioButton2">Linux</string>  
  7.     <string name="RadioButton3">Moc os</string>  
  8.     <string name="RadioButton4">Java</string>  
  9. </resources>  

 

猜你喜欢

转载自zhangyf1987hb.iteye.com/blog/1775188