Android UI之RadioGroup、RadioButton组件

RadioGroup、RadioButton:

下面看一下我们今天写的例子:

radio_layout.xml文件内容:

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

   

    <ScrollView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">

   

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Radio Demo" />

   

    <RadioGroup

        android:id="@+id/sexRg"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:checkedButton="@+id/female"

        >

        <RadioButton

            android:id="@+id/female"

            android:text="女"/>

         <RadioButton

            android:id="@+id/male"

            android:text="男"/>

       

    </RadioGroup>

    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="addRadioButton"

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

    </LinearLayout>

    </ScrollView>

</LinearLayout>

Activity中的代码:

package cn.calss3g.activity;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

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;

public class UITest3Activity extends Activity

          implements OnCheckedChangeListener{

  

RadioGroup rg = null;

Button addBtn;

private static final String TAG = "TAG";

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.radio_layout);

       

        findViews();

        //指定某个选项被选中

        rg.check(R.id.male);

        //获取当前选项组中被选中的选项的id

        int checkedId = rg.getCheckedRadioButtonId();

        RadioButton rb = (RadioButton) this.findViewById(checkedId);

        Log.i(TAG,rb.getText().toString());

    }

   

    private void findViews(){

     rg = (RadioGroup) this.findViewById(R.id.sexRg);

     //注册监听器

     rg.setOnCheckedChangeListener(this);

    

     addBtn = (Button) this.findViewById(R.id.appendRadio);

    

     addBtn.setOnClickListener(new OnClickListener(){

     public void onClick(View v){

            

               //动态增加RadioButton

          RadioButton newRb = new RadioButton(UITest3Activity.this);

          newRb.append("不明");

          newRb.setId(100);

          //添加到RadioGroup

          rg.addView(newRb);

     }

     });

   

    }

//覆盖OnCheckedChangeListener接口的抽象方法

public void onCheckedChanged(RadioGroup group, int checkedId) {

if(group.getId()==R.id.sexRg){

RadioButton rb = (RadioButton) this.findViewById(checkedId);

  Log.i(TAG,rb.getText().toString());

  }

}

}

看看效果吧:

点击addRadioButton在看效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值