RadioGroup是Android组单选按钮控件。更具体地,使用提供的RadioGroup从组中只选择一个RadioButton的能力。当用户选择一个单选按钮前一个被选中,自动成为泛滥

在我们的例子中,我们将向你展示RadioGroupAndroid应用程序的使用

activity_main.xml:

                                                                                

的RadioGroup的基本属性是Android:checkedbutton,指定单选按钮应该是默认被选中。其他成分是从类继承。您可以从上面的代码,注意单选按钮的设置是由一个RadioGroup体现所以其组成每个配置影响单选按钮

strings.xml

<?xml version="1.0" encoding="utf-8"?>    RadioGroupExample    Settings    Choose one of the radio buttons below    Sound    Vibration    Silent    Choose

MainActivity.java:

package com.javacodegeeks.android.radiogroupexample;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private RadioGroup radioGroup;private RadioButton sound, vibration, silent; private Button button;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// find which radio button is selectedif(checkedId == R.id.silent) {Toast.makeText(getApplicationContext(), "choice: Silent", Toast.LENGTH_SHORT).show();} else if(checkedId == R.id.sound) {Toast.makeText(getApplicationContext(), "choice: Sound", Toast.LENGTH_SHORT).show();} else {Toast.makeText(getApplicationContext(), "choice: Vibration", Toast.LENGTH_SHORT).show();}}});sound = (RadioButton) findViewById(R.id.sound);vibration = (RadioButton) findViewById(R.id.vibration);    silent = (RadioButton) findViewById(R.id.silent);    textView = (TextView) findViewById(R.id.text);        button = (Button)findViewById(R.id.chooseBtn);    button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {int selectedId = radioGroup.getCheckedRadioButtonId();// find which radioButton is checked by idif(selectedId == sound.getId()) {textView.setText("You chose 'Sound' option");} else if(selectedId == vibration.getId()) {textView.setText("You chose 'Vibration' option");} else {textView.setText("You chose 'Silent' option");}}    });}}

现在,让我们来看看上面的代码。当选中单选按钮在它的组改变时, OnCheckedChangeListener是为了处理这种情况调用。此接口的onCheckedChanged ( )方法,包括选择,导致回调的调用单选按钮的唯一ID 。在这个例子中,我们会告诉你选择的选择信息(例如,当按下按钮)的另一种方式。这可以通过getCheckedRadioButtonId (),它是RadioGroup中类的公共函数来完成。这个方法返回从小组选择的单选按钮的唯一ID 。你可以看看代码,看看你能如何处理这两种情况。当然, Android系统给我们提供了改变和处理的应用程序视图的属性更动态的方式。作为先决条件,是每一个映射视图中使用XML的唯一ID的组成部分。这可以通过findViewById ()方法来进行。最效果图:
相关关键词:  大神网android百度网盘教程




更多相关文章

  1. 【android】Eclipse ADT配置
  2. Android中给Button加上selector——点击按钮后变成不一样的图片
  3. ubuntu创建wifi热点(android可识别)亲测可用
  4. 【Android】Android(安卓)UI 开发_问答_3
  5. [转]android Android(安卓)SDK Setup的使用及遇到的问题,Faild t
  6. 浅谈android的selector背景选择器
  7. Android: 如何创建AVD以及选择合适target类型
  8. [Android]为Spinner填充数据后设置默认值的问题
  9. Android的selector,背景选择器

随机推荐

  1. 通过Setters方式对日期属性及日期格式进
  2. 访问JSP文件或者Servlet文件时提示下载的
  3. JSP程序使用JDBC连接MySQL的教程
  4. 实例演示函数参数与返回值、演示模板字面
  5. 1. 实例演示函数参数与返回值 2. 实例演
  6. 常用函数类型和数据类型
  7. 移动端布局实操
  8. 如何用PHP实现分布算法之一致性哈希算法
  9. PHP的命令行扩展Readline相关函数的使用
  10. 访问器属性原理与应用场景,获取 dom 元素