单选项框RadioGroup的综合应用

 大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题).

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

下面我们看一下效果图:

 

  

 

strings.xml:

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

    <string name="hello">Hello World, RadioGroupDemoActivity!</string>
    <string name="app_name">RadioGroupDemo</string>
    <string name="tr_radio_op1">帅哥</string>
      <string name="tr_radio_op2">美女</string>
      <string name="str_radio_question1">请问你是?</string>
</resources>

main.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" >

   <!--第一個TextView -->
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="228px"
    android:layout_height="49px"
    android:text="@string/str_radio_question1"
    android:textSize="30sp"   
  />   
<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:checked="true"
      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>

RadioGroupDemoActivity.java

package com.lp.ecjtu;

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

public class RadioGroupDemoActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView txt;
    private RadioGroup radiogroup;
    private RadioButton rbtn1,rbtn2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt = (TextView) findViewById(R.id.myTextView);
        radiogroup = (RadioGroup) findViewById(R.id.myRadioGroup);
        rbtn1 = (RadioButton) findViewById(R.id.myRadioButton1);
        rbtn2 = (RadioButton) findViewById(R.id.myRadioButton2);
        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //当选择的是第一个单选按钮的时候,把第一个单选按钮的内容传到TextView里面进行显示
                if(checkedId == rbtn1.getId()){
                    txt.setText(rbtn1.getText());
                }else if(checkedId == rbtn2.getId()){
                    txt.setText(rbtn2.getText());
                }
            }
        });
    }
}

转载于:https://www.cnblogs.com/200911/p/3335749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值