zoukankan      html  css  js  c++  java
  • RadioGroup实现单选并获得所选项值

    上面是一个TextView,下面有个RadioGroup,布局如下:

    主布局 main.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 -->
        <TextView
            android:id="@+id/myTextView"
            android:layout_width="228px"
            android:layout_height="49px"
            android:text="@string/str_radio_question1"
            android:textSize="30sp"
            />
        <!--建立一个RadioGroup -->
        <RadioGroup
            android:id="@+id/myRadioGroup"
            android:layout_width="137px"
            android:layout_height="216px"
            android:orientation="vertical">
            <!--第一个RadioButton -->
            <RadioButton
                android:id="@+id/myRadioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tr_radio_op1"
                />
            <!--第二个RadioButton -->
            <RadioButton
                android:id="@+id/myRadioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tr_radio_op2"
                />
        </RadioGroup>
    </LinearLayout>

    主控制程序 RadioGroupDemo.java

    package com.android.test;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
     
    public class RadioGroupDemo extends Activity {
        public TextView mTextView1;
        public RadioGroup mRadioGroup1;
        public RadioButton mRadio1, mRadio2;
     
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            // 取得 TextView、RadioGroup、RadioButton对象
            mTextView1 = (TextView) findViewById(R.id.myTextView);
            mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
            mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
            mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
     
            // RadioGroup用OnCheckedChangeListener来运行
            mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
        }
     
        private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if (checkedId == mRadio1.getId()) {
                    // 把mRadio1的内容传到mTextView1
                    mTextView1.setText(mRadio1.getText());
                } else if (checkedId == mRadio2.getId()) {
                    // 把mRadio2的内容传到mTextView1
                    mTextView1.setText(mRadio2.getText());
                }
            }
        };
    }
    

      需要注意的就是RadioGroup的消息处理。
    mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);

     

    出处:http://www.android-study.com/jichuzhishi/369.html

  • 相关阅读:
    12月15日,progress_dispaly
    Android studio和Genymotion-VirtualBox的配合使用
    JDK7动态代理源码分析
    跟踪mqttv3源码(二)
    跟踪mqttv3源码(一)
    Spring自定义标签
    Eclipse发布Maven项目到远程服务器
    结合实际项目分析pom.xml
    Maven的安装环境配置
    PHP7新特性
  • 原文地址:https://www.cnblogs.com/gzggyy/p/2516756.html
Copyright © 2011-2022 走看看