Android按鍵學習:RadioButton – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

源程序來自網絡,做瞭簡單的美化和修改。

 

HelloActivity.java

 

 

<span style="font-size:18px;">package sg131971.hello; 

 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.Gravity; 

import android.widget.RadioButton; 

import android.widget.RadioGroup; 

import android.widget.Toast; 

import android.widget.RadioGroup.OnCheckedChangeListener; 

 

public class HelloActivity extends Activity { 

    /** Called when the activity is first created. */ 

    protected RadioGroup group; 

    protected RadioButton radio1,radio2,radio3,radio4; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

         

        group = (RadioGroup)findViewById(R.id.radiogroup); 

        radio1 = (RadioButton)findViewById(R.id.button1); 

        radio2 = (RadioButton)findViewById(R.id.button2); 

        radio3 = (RadioButton)findViewById(R.id.button3); 

        radio4 = (RadioButton)findViewById(R.id.button4); 

         

        group.setOnCheckedChangeListener(new AnswerListener() ); 

    } 

     

    class AnswerListener implements OnCheckedChangeListener { 

 

        public void onCheckedChanged(RadioGroup group, int checkedId) { 

            // TODO Auto-generated method stub 

                if (checkedId == radio2.getId()) 

                { 

                    showMessage("正確答案:" + radio2.getText()+".恭喜你,答對瞭!"); 

                } 

                else if(checkedId == radio1.getId()) 

                { 

                    showMessage("對不起!" + radio1.getText()+"雖然很多,但不是公認的最多!"); 

                } 

                else if(checkedId == radio3.getId()) 

                { 

                    showMessage("對不起!" + radio3.getText()+"雖然很多,但不是公認的最多!"); 

                } 

                else 

                { 

                    showMessage("對不起!" + radio4.getText()+"雖然很多,但不是公認的最多!"); 

                } 

            } 

        }            

   

     

    public void showMessage(String str) 

    { 

        Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT); 

        toast.setGravity(Gravity.TOP, 0, 420); 

        toast.show(); 

    } 

}</span> 

 

main.xml

 

 

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:background="@drawable/bg1" 

    > 

    <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> 

    <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"> 

        <RadioButton android:layout_height="wrap_content" android:text="杭州" android:layout_width="wrap_content" android:id="@+id/button1"></RadioButton> 

        <RadioButton android:layout_height="wrap_content" android:text="重慶" android:layout_width="wrap_content" android:id="@+id/button2"></RadioButton> 

        <RadioButton android:layout_height="wrap_content" android:text="成都" android:layout_width="wrap_content" android:id="@+id/button3"></RadioButton> 

        <RadioButton android:layout_height="wrap_content" android:text="香港" android:layout_width="wrap_content" android:id="@+id/button4"></RadioButton> 

    </RadioGroup> 

</AbsoluteLayout> 

</span> 

 

運行效果圖:

 

 

作者 sg131971

 

 

發佈留言