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

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

activity_main.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:id="@+id/text"        android:text="@string/ChoiceText" />        <RadioGroup         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/text"        android:id="@+id/myRadioGroup"        android:background="#abf234"        android:checkedButton="@+id/sound" >                <RadioButton             android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/sound"        android:text="@string/Sound" />                <RadioButton             android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/vibration"        android:text="@string/Vibration" />                <RadioButton             android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/silent"        android:text="@string/Silent" />            </RadioGroup>        <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/myRadioGroup"        android:layout_marginTop="10dp"        android:id="@+id/chooseBtn"        android:text="@string/Choose" /></RelativeLayout>

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

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">RadioGroupExample</string>    <string name="action_settings">Settings</string>    <string name="ChoiceText">Choose one of the radio buttons below</string>    <string name="Sound">Sound</string>    <string name="Vibration">Vibration</string>    <string name="Silent">Silent</string>    <string name="Choose">Choose</string></resources>


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 单选按钮组的使用相关关键词:  大神网android百度网盘教程

相关关键词:  大神网android百度网盘教程



更多相关文章

  1. apk安装法之二----一段Android实现应用下载并自动安装apk包的代
  2. android:动态创建多个按钮 并给每个按键添加监听事件
  3. Android 通知的基本用法示例代码
  4. 简单的中间文字两边按钮
  5. Android使用代码实现RelativeLayout,LinearLayout布局
  6. Android 从代码中打开淘宝和亚马逊、facebook、Message
  7. 修改dialog背景以及代码扩展AlertDialog和tint的使用
  8. Android ImageSwitcher 实现按钮的3d旋转效果
  9. 【Android】Android 彩信发送的两种方式+源代码

随机推荐

  1. Android实现静默安装与卸载
  2. 谈谈Android的so
  3. Android studio快捷键大全+Android studi
  4. Android(安卓)http 请求——AsyncHttpCli
  5. android下httpd 代理设置和webview代理设
  6. [Android] Android开发规范
  7. 2012:Android关键而危险的“升级”之年
  8. android下usb框架系列文章---(4)android
  9. Android5.1禁止状态栏下拉(SystemUI Statu
  10. Android(安卓)4.4不能启动问题的解决