`
flyfox1982
  • 浏览: 78819 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

android widget 之RadioGroup RadioButton

阅读更多

RadioGroup + RadioButton 提供了一种多选一的模式。下面是xml 文件:

 

<?xml version = "1.0" encoding = "utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView android:text = "请选择你的性别"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

   

    <RadioGroup  android:layout_width="fill_parent"

                 android:layout_height="wrap_content"

                 android:orientation="vertical"

                 android:id="@+id/rg1">

    <RadioButton android:text="男" android:id="@+id/rb1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

    <RadioButton android:text="女" android:id="@+id/rb2"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"/>

    <RadioButton android:text="不男不女" android:id="@+id/rb3"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"/>

    </RadioGroup>

</LinearLayout>

 

改配置中定义了一个RadioGroup,而这个Group包括了3个RadioButton。性别对于人来说,只可能是一种,所以适合用RadioButton。需要指出的是,如果RadioButton不放在一个RadioGroup中,是不存在互斥的效果的。

 

RadioGroup 支持 setOnCheckedChangeListener,也就是说,改Group下面的选择发生变化的时候,RadioGroup可以监听到这种变化。

 

RadioButton 也支持setOnCheckedChangeListener。 不过他们对应的Listener不同,RadioGroup 是:RadioGroup.OnCheckedChangeListener,而RadioButton是:

CompoundButton.OnCheckedChangeListener.

RadioGroup.getCheckedRadioButtonId(),可以得到选择的RadioButton的ID。

 

代码如下:

 

package org.terry;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.CompoundButton;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

 

public class RadioBoxDemo extends Activity{

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.radiobox);

       

        final RadioGroup rg1 = (RadioGroup)findViewById(R.id.rg1);

        RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);

        RadioButton rb2 = (RadioButton) findViewById(R.id.rb2);

        RadioButton rb3 = (RadioButton) findViewById(R.id.rb3);

       

        Button btn1 = (Button)findViewById(R.id.rbtn1);

       

      rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

       

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton rb = (RadioButton) findViewById(checkedId);

            Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();

        }

      });

     

      rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            RadioButton rb = (RadioButton) buttonView;

            Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();

        }

    });

     

      btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            RadioButton rb = (RadioButton) findViewById(rg1.getCheckedRadioButtonId());

            Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();

        }

    });

       

    }

} }



 

 

  • 大小: 15 KB
0
0
分享到:
评论

相关推荐

    Android RadioGroup和RadioButton控件简单用法示例

    本文实例讲述了Android RadioGroup和RadioButton控件简单用法。分享给大家供大家参考,具体如下: RadioGroup和RadioButton代表的是Android中单选按钮的一种控件,写个简单的代码熟悉一下: import android.app....

    Android编程开发之RadioGroup用法实例

    本文实例讲述了Android编程开发之RadioGroup用法。分享给大家供大家参考,具体如下: RadioGroup 有时候比较有用.主要特征是给用户提供多选一机制。 MainActivity.java package com.example.lesson16_radio; ...

    安卓控件的使用例子

    import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.os.Bundle; import android.app.Activity; import android....

    Android编程单选项框RadioGroup综合应用示例

    首先,我们先设计一个TextView Widget ,以及一个RadioGroup ,并将该RadioGroup 内放置两个RadioButton ,默认为都不选择,在程序运行阶段,利用onCheckedChanged 作为启动事件装置,让User选择其中一个按钮,显示被选择的...

    android顶部滑动导航

    import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends FragmentActivity { private HorizontalScrollView scrollview; private RadioGroup radioGroup; private ...

    Android简明应用程序开发[原创]

    4.2.1 RadioButton与RadioGroup 96 4.2.2 CheckBox 100 4.2.3 CheckedTextView 103 4.2.4 Spinner 107 4.2.5 SeekBar 114 三、自动完成类Widget 119 4.3.1 AutoCompleteTextView 119 4.3.2 ...

    android开发揭秘PDF

    4.2.5 单项选择(RadioGroup、RadioButton 4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动提示(AutoComplete.TextⅥew) 4.2.9 日期和时间(DatePicker、TimePicker) 4.2.10 按钮(Button) 4.2.1l 菜单...

    Android代码-android-segmented-control

    Android-Segmented is a custom view for Android which is based on RadioGroup and RadioButton widget. This implementation is inspired by Segmented Controls for iOS. Including in your project: Download ...

    《Android应用开发揭秘》附带光盘代码.

     4.2.5 单项选择(RadioGroup、RadioButton  4.2.6 多项选择(CheckBox)  4.2.7 下拉列表(Spinner)  4.2.8 自动提示(AutoComplete.TextⅥew)  4.2.9 日期和时间(DatePicker、TimePicker)  4.2.10 按钮(Button) ...

    《Android应用开发揭秘》源码

     4.2.5 单项选择(RadioGroup、RadioButton  4.2.6 多项选择(CheckBox)  4.2.7 下拉列表(Spinner)  4.2.8 自动提示(AutoComplete.TextⅥew)  4.2.9 日期和时间(DatePicker、TimePicker)  4.2.10 按钮(Button) ...

    Android应用开发揭秘pdf高清版

    4.2.5 单项选择(RadioGroup、RadioButton 4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动提示(AutoComplete.TextⅥew) 4.2.9 日期和时间(DatePicker、TimePicker) 4.2.10 按钮(Button) 4.2.1l 菜单...

    炫舞吧 android 游戏开发

    import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity { AnimView mAnimView = null; public void onConfigurationChanged(Configuration newConfig) { ...

    Android基础知识详解

    单项选择(RadioGroup、RadioButton) 69 复选框(CheckBox) 71 开关状态按钮(ToggleButton) 73 下拉列表框Spinner 74 ScrollView、HorizontalScrollView 77 垂直滚动(ScrollView) 77 水平滚动(HorizontalScrollView...

Global site tag (gtag.js) - Google Analytics