skywang12345

导航

 

Android控件之RadioButton

RadioButton示例

创建一个activity,包含3个RadioButton:默认选中第一个;并且点击每个button都会给出相应的提示语。

应用层代码

package com.skywang.control;

import android.os.Bundle;
import android.app.Activity;
import android.widget.RadioButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;

public class RadioButtonTest extends Activity implements View.OnClickListener{
    private static final String TAG="SKYWANG";

    private RadioButton mRadioOne;
    private RadioButton mRadioTwo;
    private RadioButton mRadioThree;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_button_test);
        
        // RadioButton one
        mRadioOne = (RadioButton) findViewById(R.id.radio_one);
        mRadioOne.setOnClickListener(this);

        // RadioButton two
        mRadioTwo = (RadioButton) findViewById(R.id.radio_two);
        mRadioTwo.setOnClickListener(this);

        // RadioButton three        
        mRadioThree = (RadioButton) findViewById(R.id.radio_three);
        mRadioThree.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        boolean checked = ((RadioButton) v).isChecked();
        switch (v.getId()) {
            case R.id.radio_one:{
                Toast.makeText(getApplicationContext(), getString(R.string.text_one), Toast.LENGTH_LONG).show();
                break;        
            }
            case R.id.radio_two:{
                Toast.makeText(getApplicationContext(), getString(R.string.text_two), Toast.LENGTH_LONG).show();
                break;
            }
            case R.id.radio_three:{
                Toast.makeText(getApplicationContext(), getString(R.string.text_three), Toast.LENGTH_LONG).show();
                break;
            }
            default:
                break;
        }
    }
}

 

layout文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <RadioButton android:id="@+id/radio_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/text_one"/>
        
        <RadioButton android:id="@+id/radio_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_two"/>
    
        <RadioButton android:id="@+id/radio_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_three"/>
        
    </RadioGroup>
    
</LinearLayout>

 

manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skywang.control"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skywang.control.RadioButtonTest"
            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>

 

点击下载:源代码

 

运行效果图:

 

posted on 2013-06-15 15:44  如果天空不死  阅读(1153)  评论(0编辑  收藏  举报