当前位置:操作系统 > 安卓/Android >>

Android按键学习:RadioButton

 

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

 

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> 

 

运行效果图:

 

 


<

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,