Android虎头金刀之RadioButton之神仙妖怪

传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229


虎头金刀

        金刀驸马郭靖“他于大汗所赐金珠一介不取,连同那柄虎头金刀都留在帐中,除下元帅服色,换上了寻常皮裘。”

        今天我们学习如何利用Android平台“虎头金刀”RadioButton、RadioGroup来实现性别选择(多选一)。在实际生活中,用户填写注册信息、问卷调查等都会使用到。下面给出该情景的案例:

1案例技术要点

android.widget.RadioGroup:提供一组单选选项,实现各个选项状态的统一管理。
radioGroup.getChildCount():获取按钮组中子选项的个数
radioGroup.getChildAt(index):按索引值返回按钮组中各个子选项

2案例代码陈列

2.1AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.radiobutton"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".RadioButtonMainActivity"
            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>

2.2strings.xml

<resources>
    <string name="app_name">RadioButton选择性别</string>
    <string name="sex">性别:</string>
    <string name="man">男</string>
    <string name="woman">女</string>
    <string name="select">选择</string>
</resources>

2.3main.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sex" />
    <RadioGroup 
        android:id="@+id/sex_rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/man"/>
        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/woman"/>
    </RadioGroup>
    <Button
        android:id="@+id/select_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/select" />
</LinearLayout>

2.4RadioButtonMainActivity.java

package com.android.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * RadioButton案例:性别选择
 * 如果想在选中某一个选项按钮后,其它的选项按钮都被设置为未选中的状态,那么需要将RadioButton放置于RadioGroup中统一管理
 * @author lynnli1229
 */
public class RadioButtonMainActivity extends Activity {
    private RadioGroup sexGroup;
    private Button selectButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sexGroup = (RadioGroup) findViewById(R.id.sex_rg);
        selectButton = (Button) findViewById(R.id.select_btn);
        selectButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取单选按钮组的选项个数
                int len = sexGroup.getChildCount();
                String text = "";
                for (int i = 0; i < len; i++) {
                    RadioButton radioButton = (RadioButton) sexGroup.getChildAt(i);
                    if(radioButton.isChecked()) {
                        text = radioButton.getText().toString();
                        break;
                    }
                }
                Toast.makeText(RadioButtonMainActivity.this, text, Toast.LENGTH_LONG).show();
            }
        });
    }

}

3案例效果展示

 
posted @ 2013-05-28 08:34  Innosight  阅读(222)  评论(0编辑  收藏  举报