Android入门--RadioGroup 组与onCheckedChanged 事件

本文将引导Android初学者了解如何新建工程,并深入讲解RadioGroup组件的使用,包括在string.xml中添加字符串,以及如何在activity_forward.xml布局文件和Forward.java中处理onCheckedChanged事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

① 新建工程

② string.xml 中添加字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>
      <string name="app_name">Forward</string>
      <string name="iam_Boy">帅哥</string>
      <string name="iamGirl">美女</string>
      <string name="ask">请问你是??</string>
</resources>

activity_forward.xml代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hanji.forward.Forward" >

    <TextView
        android:id="@+id/TextView_Ask_And_Show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ask"
        android:textSize="25px" />

    <RadioGroup
        android:id="@+id/RadioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView_Ask_And_Show"
        android:layout_marginTop="20dp" >

        <RadioButton
            android:id="@+id/RadioButton_Boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/iam_Boy" >
        </RadioButton>

        <RadioButton
            android:id="@+id/RadioButton_Gril"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/iamGirl" >
        </RadioButton>
    </RadioGroup>

</RelativeLayout>

Forward.java代码如下:

package com.hanji.forward;

import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Forward extends ActionBarActivity {

	/** Called when the activity is first created. */
	private TextView answer_TextView;
	private RadioButton boy_RadioButton, girl_RadioButton;
	private RadioGroup radioGroup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forward);

		/* findViewById()从XML中获取资源对象 */
		answer_TextView = (TextView) findViewById(R.id.TextView_Ask_And_Show);
		radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
		boy_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Boy);
		girl_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Gril);
		/* 给单RadioGroup添加状态改变监听器 */
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (boy_RadioButton.isChecked()) {
					answer_TextView.setText(R.string.iam_Boy);
				} else {
					answer_TextView.setText(R.string.iamGirl);
				}
			}
		});
	}
}
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值