例子效果图

 

 

 

实现步骤

 

第一步:建Android 工程:RadioGroupDemo

                                                                   

第二步:编写Activity 的子类别:RadioGroupDemo,其程序代码如下:

 

package com.a3gs.radiogroup;

 

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 {

    private TextView TV;

    private RadioGroup RG;

    private RadioButton RB1, RB2;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        TV = (TextView) findViewById(R.id.TV);

        RG = (RadioGroup) findViewById(R.id.RG);

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

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

       

        RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

 

           @Override

           public void onCheckedChanged(RadioGroup group, int checkedId) {

              // TODO Auto-generated method stub

              if(checkedId == RB1.getId()){

                  TV.setText("您所选择的是:" + RB1.getText().toString());

              }

              if(checkedId == RB2.getId()){

                  TV.setText("您所选择的是:" + RB2.getText().toString());

              }

           }

           

        });

    }

}

 

第三步:修改res/layout/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 

    android:id="@+id/TV"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

<RadioGroup

    android:id="@+id/RG"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical"

    >

    <RadioButton

    android:id="@+id/rb1"

    android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/rbText1"

    />

    <RadioButton

    android:id="@+id/rb2"

    android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/rbText2"

    />

RadioGroup>

LinearLayout>

 

第四步:修改res/values/ strings.xml,其代码如下:

 

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

<resources>

    <string name="hello">Hello World, RadioGroupDemo!string>

    <string name="app_name">RadioGroupDemostring>

    <string name="rbText1">天使知识授理厂string>

    <string name="rbText2">www.a3gs.comstring>

resources>

更多相关文章

  1. Android超炫日期日历控件:TimesSquare
  2. JS调用Java代码(Android)
  3. 客制化android activity的title
  4. Android关闭多个activity
  5. PreferenceActivity、PreferenceFragment使用
  6. Android应用程序进程启动过程的源代码分析(2)
  7. Android(安卓)的 Button 按钮实现的两种方式
  8. android 常用组建案例
  9. Android优质学习方法

随机推荐

  1. Unable to execute dex: Multiple dex fi
  2. android scrollview组件禁止滑动的方法
  3. 图片的ScaleType详解 ImageView的属性and
  4. android随意记
  5. Android(安卓)onSaveInstanceState和onRe
  6. Android客户端登录会话保持现实的文章汇
  7. android light
  8. [Android]libpng的编译与使用
  9. Android启动画面实现
  10. Android(安卓)禁止下拉状态栏和底部导航