android RadioGroup(单项选择)

RadioGroup01.JAVA:

package com.RadioGroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioGroup01 extends Activity {
 private TextView mTextView;
 private RadioGroup mRadioGroup;
 private RadioButton mRadioButton01,mRadioButton02,mRadioButton03,mRadioButton04;
 
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mTextView=(TextView)findViewById(R.id.mTextView);
        mRadioGroup=(RadioGroup)findViewById(R.id.mRadioGroup);
        mRadioButton01=(RadioButton)findViewById(R.id.mRadioButton01);
        mRadioButton02=(RadioButton)findViewById(R.id.mRadioButton02);
        mRadioButton03=(RadioButton)findViewById(R.id.mRadioButton03);
        mRadioButton04=(RadioButton)findViewById(R.id.mRadioButton04);
       
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         
         @Override
         public void onCheckedChanged(RadioGroup group,int checkId){
          if(checkId==mRadioButton02.getId())
          {
           DisplayToast("Right answer:"+mRadioButton02.getText());
          }else{
           DisplayToast("Fuck you");
          }
         }
        });
    }
    public void DisplayToast(String str){
     Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
     toast.setGravity(Gravity.BOTTOM, 0, 200);
     toast.show();
    }
}

 

 

main.xml:

<?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:id="@+id/mTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<RadioGroup
 android:id="@+id/mRadioGroup"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 >
<RadioButton
 android:id="@+id/mRadioButton01"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mRadioButton01"
 />
<RadioButton
    android:id="@+id/mRadioButton02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mRadioButton02"
    />
<RadioButton
    android:id="@+id/mRadioButton03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mRadioButton03"
    />
<RadioButton
    android:id="@+id/mRadioButton04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mRadioButton04"
    />
</RadioGroup>

</LinearLayout>

 

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.RadioGroup"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".RadioGroup01"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>


</manifest>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值