API Demos 2.3 学习笔记 (15)-- Views->Radio Group

更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》


想想我们上学时候做的单项选择题,其中只有一个是正确答案。在做题的时候,我们只能选择一项。如果我们想在Android上设计一道单项选择题的话,可能就要用到RadioGroup了。RadioGroup常常和RadioButton一起使用。由一个RadioGroup包含若干个RadioButton,组成一个单项选择群组。我们在同一时间只能选中该组中的一个 RadioButton。
RadioGroup的创建主要有两种方法:

1、在xml布局文件中 

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:checkedButton="@+id/lunch"
        android:id="@+id/menu">
        <RadioButton
            android:text="@string/radio_group_1_breakfast"
            android:id="@+id/breakfast"
            />
        <RadioButton
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch" />
        <RadioButton
            android:text="@string/radio_group_1_dinner"
            android:id="@+id/dinner" />
        <RadioButton
            android:text="@string/radio_group_1_all"
            android:id="@+id/all" />
    </RadioGroup>
注:上面演示的是在一个RadioGroup中包含四个RadioButton的情况,您可以根据自己实际需要来增加或者减少RadioButton的个数。



2、在Java代码中

        // 通过findViewById方法获得一个RadioGroup对象        
        RadioGroup  mRadioGroup = (RadioGroup) findViewById(R.id.menu);
        
        // 向RadioGroup中动态添加一个RadioButton对象
        RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setId(R.id.snack);
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 0, layoutParams);
注:这里我们首先创建了一个 RadioGroup对象和一个 RadioButton对象,然后通过 addView函数将 RadioButton添加到RadioGroup中。其中 addView函数的第二个参数,表示将 RadioButton添加到RadioGroup内部的什么位置(从0开始)。 RadioButton通过 setText和 setId分别设定文本和资源ID号码。


当我们做单项选择题的时候,可以很快看到选择的是哪个选项。但是在Android中使用RadioGroup时,我们怎么通过程序来获得用户到底选择的是哪个选项呢?这就要用到我们常用的方法(监听器)了。具体使用方法如下:

	// 通过findViewById方法获得一个RadioGroup对象        
	RadioGroup  mRadioGroup = (RadioGroup) findViewById(R.id.menu);

	//当RadioButton状态发生改变时,触发监听器,执行下面的动作。当清除选择项时,checkedId为-1。
	mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				String choice = null;
				//根据checkedId判断用户选择了哪一个选项,并执行相应的动作。
				switch (checkedId) {
					case R.id.breakfast: 
						choice = "breakfast";
						break;
					case R.id.lunch:
						choice = "lunch";
						break;
					case R.id.dinner:
						choice = "dinner";
						break;
					case R.id.all:
						choice = "all";
						break;		
					case R.id.snack:
						choice = "snack";
						break;	   		
					default:
						break;
				}				
			}
		});
注:我们可以根据监听器中的 checkedId来判断是哪一个选项被选择,并执行相应的动作。


下面我们进行实例代码解析:

res-values-string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="radio_group_1_breakfast">Breakfast</string>
    <string name="radio_group_1_lunch">Lunch</string>
    <string name="radio_group_1_dinner">Dinner</string>
    <string name="radio_group_1_all">All of them</string>
    <string name="radio_group_1_selection">You have selected: (none)</string>
    <string name="radio_group_1_clear">Clear</string>
</resources>

res-layout-radio_group_1.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">
   <!--  一个RadioGroup(单项选择组)对象,内含4个RadioButton(单项选择按钮)和一个TextView对象 -->
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:checkedButton="@+id/lunch"
        android:id="@+id/menu">
        <RadioButton
            android:text="@string/radio_group_1_breakfast"
            android:id="@+id/breakfast"
            />
        <RadioButton
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch" />
        <RadioButton
            android:text="@string/radio_group_1_dinner"
            android:id="@+id/dinner" />
        <RadioButton
            android:text="@string/radio_group_1_all"
            android:id="@+id/all" />
        <TextView
            android:text="@string/radio_group_1_selection"
            android:id="@+id/choice" />
    </RadioGroup>
    
    <!-- 一个Button对象,用于清除RadioGroup的选择项 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_clear"
        android:id="@+id/clear" />
</LinearLayout>

src-com.example.android.apis.view-RadioGroup1.java

package com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.LinearLayout;


public class RadioGroup1 extends Activity implements RadioGroup.OnCheckedChangeListener,
        View.OnClickListener {

    private TextView mChoice;
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.radio_group_1);

        // 通过findViewById方法获得一个RadioGroup对象        
        mRadioGroup = (RadioGroup) findViewById(R.id.menu);

        // 向RadioGroup中动态添加一个RadioButton对象
        RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setId(R.id.snack);
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 0, layoutParams);

        //为RadioGroup添加监听器,当点击RadioButton时,会触发监听器, 执行onCheckedChanged中的动作
        mRadioGroup.setOnCheckedChangeListener(this);
        
        //在TextView控件上显示被选择项的提示信息
        String selection = getString(R.string.radio_group_selection);
        mChoice = (TextView) findViewById(R.id.choice);
        mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());

        // 通过findViewById方法获得一个Button对象,点击该对象会清除RadioGroup中的选择项
        Button clearButton = (Button) findViewById(R.id.clear);
        clearButton.setOnClickListener(this);
    }

    //当RadioButton状态发生改变时,触发监听器,执行下面的动作。当清除选择项时,checkedId为-1。
    public void onCheckedChanged(RadioGroup group, int checkedId) {
    	String selection = getString(R.string.radio_group_selection);
    	String none = getString(R.string.radio_group_none);
    	String choice = null;
    	//根据checkedId判断用户选择了哪一个选项,并执行相应的动作。
    	switch (checkedId) {
    	case R.id.breakfast: 
    		choice = "breakfast";
    		break;
    	case R.id.lunch:
    		choice = "lunch";
    		break;
    	case R.id.dinner:
    		choice = "dinner";
    		break;
    	case R.id.all:
    		choice = "all";
    		break;		
    	case R.id.snack:
    		choice = "snack";
    		break;	   		
    	default:
    		break;
    	}
    	mChoice.setText(selection + choice +
    			(checkedId == View.NO_ID ? none : checkedId));       
    }

    //点击clearButton时,清空所有RadioButton的选择状态
    public void onClick(View v) {
        mRadioGroup.clearCheck();
    }
}


欢迎大家关注我的微信公众号:

微信公众号:sn0wdr1am


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值