Android按键学习:RadioButton


源程序来自网络,做了简单的美化和修改。

HelloActivity.java

  1. package sg131971.hello;  
  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.Toast;  
  9. import android.widget.RadioGroup.OnCheckedChangeListener;  
  10.   
  11. public class HelloActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     protected RadioGroup group;  
  14.     protected RadioButton radio1,radio2,radio3,radio4;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         group = (RadioGroup)findViewById(R.id.radiogroup);  
  21.         radio1 = (RadioButton)findViewById(R.id.button1);  
  22.         radio2 = (RadioButton)findViewById(R.id.button2);  
  23.         radio3 = (RadioButton)findViewById(R.id.button3);  
  24.         radio4 = (RadioButton)findViewById(R.id.button4);  
  25.           
  26.         group.setOnCheckedChangeListener(new AnswerListener() );  
  27.     }  
  28.       
  29.     class AnswerListener implements OnCheckedChangeListener {  
  30.   
  31.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  32.             // TODO Auto-generated method stub   
  33.                 if (checkedId == radio2.getId())  
  34.                 {  
  35.                     showMessage("正确答案:" + radio2.getText()+".恭喜你,答对了!");  
  36.                 }  
  37.                 else if(checkedId == radio1.getId())  
  38.                 {  
  39.                     showMessage("对不起!" + radio1.getText()+"虽然很多,但不是公认的最多!");  
  40.                 }  
  41.                 else if(checkedId == radio3.getId())  
  42.                 {  
  43.                     showMessage("对不起!" + radio3.getText()+"虽然很多,但不是公认的最多!");  
  44.                 }  
  45.                 else  
  46.                 {  
  47.                     showMessage("对不起!" + radio4.getText()+"虽然很多,但不是公认的最多!");  
  48.                 }  
  49.             }  
  50.         }             
  51.     
  52.       
  53.     public void showMessage(String str)  
  54.     {  
  55.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  56.         toast.setGravity(Gravity.TOP, 0, 420);  
  57.         toast.show();  
  58.     }  
  59. }  

main.xml

[javascript]

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout 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.     android:background="@drawable/bg1"  
  7.     >  
  8.     <TextView android:text="哪个城市的美女最多?" android:textSize="30px" android:layout_height="wrap_content" android:id="@+id/mytextview" android:layout_width="fill_parent" android:textColor="#FF0000" android:layout_x="0dp" android:layout_y="80dp"></TextView>  
  9.     <RadioGroup android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radiogroup" android:layout_x="0dp" android:layout_y="140dp">  
  10.         <RadioButton android:layout_height="wrap_content" android:text="杭州" android:layout_width="wrap_content" android:id="@+id/button1"></RadioButton>  
  11.         <RadioButton android:layout_height="wrap_content" android:text="重庆" android:layout_width="wrap_content" android:id="@+id/button2"></RadioButton>  
  12.         <RadioButton android:layout_height="wrap_content" android:text="成都" android:layout_width="wrap_content" android:id="@+id/button3"></RadioButton>  
  13.         <RadioButton android:layout_height="wrap_content" android:text="香港" android:layout_width="wrap_content" android:id="@+id/button4"></RadioButton>  
  14.     </RadioGroup>  
  15. </AbsoluteLayout>  

运行效果图:

Android按键学习:RadioButton
Android按键学习:RadioButton

相关内容