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();
    }
}



相关文章
|
人工智能 监控 安全
F5社区学习笔记:API和AI如何改变应用安全?
F5社区学习笔记:API和AI如何改变应用安全?
137 1
|
jenkins API 持续交付
jenkins学习笔记之十五:SonarSQube API使用
jenkins学习笔记之十五:SonarSQube API使用
【Azure API 管理】解决API Management添加AAD Group时遇见的 Failed to query Azure Active Directory graph due to error 错误
【Azure API 管理】解决API Management添加AAD Group时遇见的 Failed to query Azure Active Directory graph due to error 错误
100 0
|
XML API 数据格式
【Qt 学习笔记】QWidget的enable属性 | API的介绍
【Qt 学习笔记】QWidget的enable属性 | API的介绍
442 0
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
218 0
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
107 0
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
176 0
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结4
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结
183 0
|
2月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南