• 沒有找到結果。

猜猜我是??

在文檔中 开放手机联盟 (頁 137-143)

① 新建项目

② 准备三张 png 图片

③ 在 string.xml 中添加要使用的字符串

④ 在 main.xml 中添加 UI 元素

ic_menu_close_clear_cancel.png right.png wrong.png

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

<resources>

<string name="app_name">Ex_Ctrl_6</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>

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

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/ask"

android:id="@+id/TextView_Ask_And_Show"

android:textSize="25px"/>

28

⑤ 修改 mainActivity.java 文件

<RadioGroup

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/RadioButton_Boy"

android:text="@string/iam_Boy">

</RadioButton>

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/iamGirl"

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

</RadioButton>

</RadioGroup>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<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>

</LinearLayout>

package package package

package zyf.Ex_Ctrl_6;

import import import

import java.util.Random;

import import import

import android.app.Activity;

import import import

import android.app.AlertDialog;

import import import

import android.os.Bundle;

import import import

import android.view.Menu;

29

import import import

import android.view.View;

import import import

import android.widget.Button;

import import import

import android.widget.RadioButton;

import import import

import android.widget.RadioGroup;

import import import

import android.widget.TextView;

import import import

import android.widget.Toast;

public public public

public classclassclassclass Ex_Ctrl_6 extendsextendsextendsextends Activity implementsimplementsimplementsimplements Button.OnClickListener {

/** Called when the activity is first created. */

private private private

private TextView answer_TextView;

private private private

private RadioButton boy_RadioButton, girl_RadioButton;

private private private

private RadioGroup radioGroup;

private private private

private Button answer_Button, clean_Button;

private private private

private String[] boy_girl;

private private private

private AlertDialog.Builder showRightorNot;

private private private

private MenuItem exit;

@Override public public public

public voidvoidvoidvoid onCreate(Bundle savedInstanceState) { super

super super

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* 结果显示对话框 */

showRightorNot = newnewnewnew AlertDialog.Builder(thisthisthisthis);

/* 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 = newnewnewnew String[] { "Boy", "Girl","Boy", "Girl",

"Boy", "Girl","Boy", "Girl",

"Boy" };

/* 按钮设置成不可选 */

answer_Button.setEnabled(falsefalsefalsefalse);

clean_Button.setEnabled(falsefalsefalsefalse);

30

/* 给单RadioGroup添加状态改变监听器 */

radioGroup.setOnCheckedChangeListener(newnewnewnew

RadioGroup.OnCheckedChangeListener() {

@Override public public public

public voidvoidvoidvoid onCheckedChanged(RadioGroup group,intintintint checkedId){

// TODOTODOTODOTODO Auto-generated method stub /* 判断显示 */

ifif

ifif (boy_RadioButton.isChecked()) {

answer_TextView.setText(R.string.iam_Boy);

} elseelseelseelse {

answer_TextView.setText(R.string.iamGirl);

} } });

boy_RadioButton.setOnClickListener(newnewnewnew RadioGroup.OnClickListener() {

@Override public public public

public voidvoidvoidvoid onClick(View v) {

// TODOTODOTODOTODO Auto-generated method stub /* 按钮设置成可选 */

answer_Button.setEnabled(truetruetruetrue);

clean_Button.setEnabled(truetruetruetrue);

} });

girl_RadioButton.setOnClickListener(newnewnewnew RadioGroup.OnClickListener() {

@Override public public public

public voidvoidvoidvoid onClick(View v) {

// TODOTODOTODOTODO Auto-generated method stub /* 按钮设置成可选 */

answer_Button.setEnabled(truetruetruetrue);

clean_Button.setEnabled(truetruetruetrue);

}

});

/* 设置按钮事件监听器 */

answer_Button.setOnClickListener(thisthisthisthis);

clean_Button.setOnClickListener(thisthisthisthis);

}

31

public public public

public voidvoidvoidvoid onClick(View v) {

// TODOTODOTODOTODO Auto-generated method stub if

if if

if (v.getId() == R.id.answer_The_Q_button) { if

if if

if (boy_RadioButton.isChecked()) { checkTheAnswer("Boy");

} elseelseelseelse ifififif (girl_RadioButton.isChecked()) { checkTheAnswer("Girl");

} } ifif

ifif (v.getId() == R.id.clean_The_Q_button) { /* 按钮设置成未选取 */

boy_RadioButton.setChecked(falsefalsefalsefalse);

girl_RadioButton.setChecked(falsefalsefalsefalse);

/* 按钮设置成不可选 */

answer_Button.setEnabled(falsefalsefalsefalse);

clean_Button.setEnabled(falsefalsefalsefalse);

answer_TextView.setText(R.string.ask);

} }

private private private

private voidvoidvoidvoid checkTheAnswer(String checkstring) { // TODOTODOTODOTODO Auto-generated method stub

/*检测提示*/

Toast.makeText(thisthisthisthis, "检测答案…………", Toast.LENGTH_SHORT).show();

/*获取随机数*/

Random random = newnewnewnew Random();

int int int

int index = random.nextInt(9);

if if if

if (boy_girl[index].equals(checkstring)) { /*回答正确 ,设置提示对话框,显示结果*/

showRightorNot.setIcon(R.drawable.right);

showRightorNot.setTitle(R.string.showAnswerTitle_YES);

showRightorNot.setPositiveButton(R.string.about_dialog_ok, nullnullnullnull);

showRightorNot.setMessage(R.string.right).show();

Toast.makeText(thisthisthisthis,getString(R.string.answerIS)

+boy_girl[index],Toast.LENGTH_LONG).show();

} elseelseelseelse {

/*回答错误 ,设置提示对话框,显示结果*/

showRightorNot.setIcon(R.drawable.wrong);

showRightorNot.setTitle(R.string.showAnswerTitle_NO);

showRightorNot.setPositiveButton(R.string.about_dialog_ok, nullnullnullnull);

32

⑥ 结果

showRightorNot.setMessage(R.string.wrong).show();

Toast.makeText(thisthisthisthis,getString(R.string.answerIS)+

boy_girl[index], Toast.LENGTH_LONG).show();

} }

@Override public public public

public booleanbooleanbooleanboolean onCreateOptionsMenu(Menu menu) { // TODOTODOTODOTODO Auto-generated method stub

/*添加退出菜单*/

exit=menu.add("Exit");

/*设置退出菜单图片*/

exit.setIcon(R.drawable.ic_menu_close_clear_cancel);

return return return

return supersupersupersuper.onCreateOptionsMenu(menu);

}

@Override public public public

public booleanbooleanbooleanboolean onOptionsItemSelected(MenuItem item) { // TODOTODOTODOTODO Auto-generated method stub

/*结束Activity*/

finish();

return return return

return supersupersupersuper.onOptionsItemSelected(item);

} }

2

Android Android

Android Android 基础 UI UI UI UI 编程 4 4 4 4

在文檔中 开放手机联盟 (頁 137-143)