① 新建工程
② 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);
}
}
});
}
}