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>
<string name="answer_Q">回答</string>
<string name="clean">清空</string>
<string name="showAnswerTitle_YES">恭喜!</string>
<string name="showAnswerTitle_NO">很遗憾!</string>
<string name="about_dialog_ok">OK</string>
<string name="right">您答对了!!</string>
<string name="wrong">哈哈,错误!</string>
<string name="answerIS">正确答案是:</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>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/RadioGroup"
android:layout_marginTop="20dp" >
<Button
android:id="@+id/answer_The_Q_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer_Q" >
</Button>
<Button
android:id="@+id/clean_The_Q_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clean" >
</Button>
</LinearLayout>
</RelativeLayout>
Forward.java代码如下:
package com.hanji.forward;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
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;
private Button answer_Button, clean_Button;
private String[] boy_girl;
private AlertDialog.Builder showRightorNot;
private MenuItem exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forward);
// * 结果显示对话框*/
showRightorNot = new AlertDialog.Builder(this);
/* 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);
answer_Button = (Button) findViewById(R.id.answer_The_Q_button);
clean_Button = (Button) findViewById(R.id.clean_The_Q_button);
/* 答案数组 */
boy_girl = new String[] { "Boy", "Girl", "Boy", "Girl", "Boy", "Girl",
"Boy", "Girl", "Boy" };
/* 按钮设置成不可选 */
answer_Button.setEnabled(false);
clean_Button.setEnabled(false);
/* 给单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);
}
}
});
boy_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 按钮设置成可选 */
answer_Button.setEnabled(true);
clean_Button.setEnabled(true);
}
});
girl_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 按钮设置成可选 */
answer_Button.setEnabled(true);
clean_Button.setEnabled(true);
}
});
/* 设置按钮事件监听器 */
answer_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.answer_The_Q_button) {
if (boy_RadioButton.isChecked()) {
checkTheAnswer("Boy");
} else if (girl_RadioButton.isChecked()) {
checkTheAnswer("Girl");
}
}
if (v.getId() == R.id.clean_The_Q_button) {
/* 按钮设置成未选取 */
boy_RadioButton.setChecked(false);
girl_RadioButton.setChecked(false);
/* 按钮设置成不可选 */
answer_Button.setEnabled(false);
clean_Button.setEnabled(false);
answer_TextView.setText(R.string.ask);
}
}
});
clean_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
// @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.answer_The_Q_button) {
if (boy_RadioButton.isChecked()) {
checkTheAnswer("Boy");
} else if (girl_RadioButton.isChecked()) {
checkTheAnswer("Girl");
}
}
if (v.getId() == R.id.clean_The_Q_button) {
/* 按钮设置成未选取 */
boy_RadioButton.setChecked(false);
girl_RadioButton.setChecked(false);
/* 按钮设置成不可选 */
answer_Button.setEnabled(false);
clean_Button.setEnabled(false);
answer_TextView.setText(R.string.ask);
}
}
private void checkTheAnswer(String checkstring) {
// TODO Auto-generated method stub
/* 检测提示 */
Toast.makeText(this, "检测答案…………", Toast.LENGTH_SHORT).show();
/* 获取随机数 */
Random random = new Random();
int index = random.nextInt(9);
if (boy_girl[index].equals(checkstring)) {
/* 回答正确,设置提示对话框,显示结果 */
showRightorNot.setIcon(R.drawable.right);
showRightorNot.setTitle(R.string.showAnswerTitle_YES);
showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);
showRightorNot.setMessage(R.string.right).show();
Toast.makeText(this,
getString(R.string.answerIS) + boy_girl[index],
Toast.LENGTH_LONG).show();
} else {
/* 回答错误,设置提示对话框,显示结果 */
showRightorNot.setIcon(R.drawable.wrong);
showRightorNot.setTitle(R.string.showAnswerTitle_NO);
showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);
showRightorNot.setMessage(R.string.wrong).show();
Toast.makeText(this,
getString(R.string.answerIS) + boy_girl[index],
Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/* 添加退出菜单 */
exit = menu.add("Exit");
/* 设置退出菜单图片 */
exit.setIcon(R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/* 结束Activity */
finish();
return super.onOptionsItemSelected(item);
}
}