码迷,mamicode.com
首页 > 移动开发 > 详细

Android小程序-乐学成语游戏(四)

时间:2016-06-21 07:54:34      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

目标效果:

技术分享 技术分享 技术分享

技术分享 技术分享

游戏页面有几个小游戏,因为时间原因只做了第一个猜成语,是用的选择题方式,十道题以内答对六题算闯关成功。


1.新建GameActivity.java页面和activity_game.xml页面,activity_game.xml作为显示游戏目录页面:

activity_game.xml页面:

<LinearLayout 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:background="@drawable/bg_ling"
    android:orientation="vertical"
    tools:context=".GameActivity" >

    <TextView
        android:id="@+id/tvGameGuess"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:clickable="true"
        android:onClick="gamePlay"
        android:text="@string/tvGameGuess"
        android:textColor="#000000"
        android:textSize="23sp" />

    <TextView
        android:id="@+id/tvGameWrite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:clickable="true"
        android:onClick="gamePlay"
        android:text="@string/tvGameWrite"
        android:textColor="#000000"
        android:textSize="23sp" />

    <TextView
        android:id="@+id/tvGameConn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:clickable="true"
        android:onClick="gamePlay"
        android:text="@string/tvGameConn"
        android:textColor="#000000"
        android:textSize="23sp" />

</LinearLayout>


2.新建GameGuessActivity.java页面和activity_game_guess.xml页面,activity_game_guess.xml页面显示猜成语游戏规则。
activity_game_guess.xml页面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_animal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvGuessHelp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        android:text="@string/tvGuessHelp"
        android:textColor="#000000"
        android:textSize="25sp" />

    <!-- android:background="#00ffffff"去掉边缘 -->

    <ImageButton
        android:id="@+id/ibStartGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="300dp"
        android:background="#00ffffff"
        android:onClick="startGame"
        android:src="@drawable/btnstart" />

</LinearLayout>


3.新建PlayGuessActivity.java页面和activity_play_guess.xml页面,activity_play_guess.xml页面作为猜成语游戏界面。
activity_play_guess.xml页面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_animal"
    android:orientation="vertical" >

    <!-- 始终使TextView显示在中间 -->

    <TextView
        android:id="@+id/tvExplain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/tvRightOrWrong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="70dp"
        android:text=" "
        android:textColor="#08c50d"
        android:textSize="22sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="60dp"
        android:orientation="vertical" >

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/rbPhraseOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:textColor="#000000"
                android:textSize="20sp" />

            <RadioButton
                android:id="@+id/rbPhraseTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:textColor="#000000"
                android:textSize="20sp" />

            <RadioButton
                android:id="@+id/rbPhraseThree"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:textColor="#000000"
                android:textSize="20sp" />

            <RadioButton
                android:id="@+id/rbPhraseFour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:textColor="#000000"
                android:textSize="20sp" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/ibSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00ffffff"
            android:onClick="AnswerSubmit"
            android:src="@drawable/btnsubmit" />

        <ImageButton
            android:id="@+id/ibNext"
            android:layout_marginLeft="70dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00ffffff"
            android:onClick="AnswerSubmit"
            android:src="@drawable/btnnext" />
    </LinearLayout>

</LinearLayout>


4.GameActivity.java页面跳转每一个游戏页面。
GameActivity.java页面:
package cn.edu.bztc.happyidion.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class GameActivity extends Activity {

	private Intent intent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
	}

	public void gamePlay(View view) {
		switch (view.getId()) {
		case R.id.tvGameGuess:
			intent=new Intent(this,GameGuessActivity.class);
			startActivity(intent);
			break;
		case R.id.tvGameConn:

			break;

		default:
			break;
		}
	}

}


5.GameGuessActivity.java页面点击编写开始游戏点击事件。
GameGuessActivity.java页面:
package cn.edu.bztc.happyidion.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class GameGuessActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_guess);
		
		
	}
	public void startGame(View view){
		switch (view.getId()) {
		case R.id.ibStartGame:
			Intent intent=new Intent(this,PlayGuessActivity.class);
			startActivity(intent);
			finish();
			break;

		default:
			break;
		}
	}
}


6.GameDao.java页面定义游戏中对成语表的查询方法。
GameDao.java页面:
package cn.edu.bztc.happyidiom.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import cn.edu.bztc.happyidiom.db.MyDatabaseHelper;
import cn.edu.bztc.happyidiom.entity.Animal;

public class GameDao {
	private static GameDao gameDao;
	private SQLiteDatabase db;
	public GameDao(Context context){
		DBOpenHelper dbHelper=new DBOpenHelper(context);
		db=dbHelper.openDatabase();
	}
	
	public synchronized static GameDao getInstance(Context context){
		if(gameDao==null){
			gameDao=new GameDao(context);
		}
		return gameDao;
	}
	
	/*根据id获取成语信息*/
	public Animal getPhrase(String id){
		Animal animal = null;
		Cursor cursor=db.query("animal",null,"_id=?",new String[]{id},null,null,null);
		if(cursor.moveToFirst()){
			animal=new Animal();
			animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			animal.setName(cursor.getString(cursor.getColumnIndex("name")));
			animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));
			animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));
			animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));
			animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));
			animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));
			animal.setExplain(cursor.getString(cursor.getColumnIndex("explain")));
		}
		cursor.close();
		return animal;
	}
}


7.PlayGuessActivity.java页面为游戏界面。
PlayGuessActivity.java页面:
package cn.edu.bztc.happyidion.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.dao.GameDao;
import cn.edu.bztc.happyidiom.entity.Animal;

import android.R.color;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;

public class PlayGuessActivity extends Activity {

	int score=0;// 总成绩
	int number=1;// 题目数目
	private TextView tvRightOrWrong;
	private ImageButton ibNext,ibSubmit;
	private Random random;// 随机数
	private AnimalDao animalDao;
	private GameDao gameDao;
	private List<Animal> animalList;
	private Animal animal, animalTwo, animalThree, animalFour;// 随机生成的成语对象
	private SharedPreferences pref;
	private Editor editor;
	private int[] array = { 5, 5, 5, 5 };// 整形数组,用于生成随机顺序显示选项
	private String answer;// 选择的答案

	private TextView tvExplain;
	private RadioButton rbPhraseOne, rbPhraseTwo, rbPhraseThree, rbPhraseFour;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play_guess);
		tvExplain = (TextView) findViewById(R.id.tvExplain);
		rbPhraseOne = (RadioButton) findViewById(R.id.rbPhraseOne);
		rbPhraseTwo = (RadioButton) findViewById(R.id.rbPhraseTwo);
		rbPhraseThree = (RadioButton) findViewById(R.id.rbPhraseThree);
		rbPhraseFour = (RadioButton) findViewById(R.id.rbPhraseFour);
		tvRightOrWrong = (TextView) findViewById(R.id.tvRightOrWrong);
		ibSubmit=(ImageButton) findViewById(R.id.ibSubmit);
		ibNext=(ImageButton) findViewById(R.id.ibNext);

		pref = getSharedPreferences("FourPhrase", MODE_PRIVATE);// 将正确成语和随机生成的三个成语保存到文档
		editor = pref.edit();// 获取SharedPreferences.Editor对象

		/* 获取随机值对应的成语解释 */
		getNumber();

		/* 显示解释到TextView */
		showPhrase();

		/* 获取三个随机成语和一个正确成语的选项 */
		getFourPhrase();

		/* 显示到RadioButton */
		showRadiButton();

	}

	/* 获取随机值对应的成语 */
	private void getNumber() {
		animalList = new ArrayList<Animal>();
		animalDao = AnimalDao.getInstance(this);
		animalList = animalDao.getAllAnimals();
		random = new Random();

		String id = Integer.toString(random.nextInt(animalList.size()));
		gameDao = GameDao.getInstance(this);
		animal = gameDao.getPhrase(id);
		
		animalList = new ArrayList<Animal>();
		animalDao = AnimalDao.getInstance(this);
		animalList = animalDao.getAllAnimals();
	}

	/* 显示解释到页面 */
	private void showPhrase() {
		tvExplain.setText(animal.getExplain());
	}

	/* 获取四个成语选项 */
	private void getFourPhrase() {
		random = new Random();

		String two = null, three = null, four = null;
		two = Integer.toString(random.nextInt(animalList.size()));
		three = Integer.toString(random.nextInt(animalList.size()));
		four = Integer.toString(random.nextInt(animalList.size()));

		gameDao = GameDao.getInstance(this);
		animalTwo = gameDao.getPhrase(two); // 获取随机生成值对应的成语
		animalThree = gameDao.getPhrase(three);
		animalFour = gameDao.getPhrase(four);

		editor.putString("0", animal.getName());// 将四个成语存入文档
		editor.putString("1", animalTwo.getName());
		editor.putString("2", animalThree.getName());
		editor.putString("3", animalFour.getName());
		editor.commit();

		int no;
		for (int i = 0; i < array.length; i++) { // 随机生成四个不重复的值作为显示顺序
			no = random.nextInt(4);
			int j;
			for (j = 0; j <= i; j++) {
				if (no == array[j]) {
					i--;
					break;
				}
			}
			if (j == i + 1) {
				array[i] = no;
			}
		}
		Log.i("MainActivity", "0:" + array[0] + "1:" + array[1] + "2:"
				+ array[2] + "3:" + array[3]);
	}

	/* 显示到RadioButton */
	private void showRadiButton() {
		rbPhraseOne.setText(pref.getString(Integer.toString(array[0]), ""));
		rbPhraseTwo.setText(pref.getString(Integer.toString(array[1]), ""));
		rbPhraseThree.setText(pref.getString(Integer.toString(array[2]), ""));
		rbPhraseFour.setText(pref.getString(Integer.toString(array[3]), ""));
	}

	/* 提交答案 */
	public void AnswerSubmit(View view) {
		switch (view.getId()) {
		case R.id.ibSubmit:
			if (rbPhraseOne.isChecked())
				answer = rbPhraseOne.getText().toString();
			else if (rbPhraseTwo.isChecked())
				answer = rbPhraseTwo.getText().toString();
			else if (rbPhraseThree.isChecked())
				answer = rbPhraseThree.getText().toString();
			else
				answer = rbPhraseFour.getText().toString();

			if (answer == animal.getName()) {
				tvRightOrWrong.setText("真棒,回答正确");
				tvRightOrWrong.setTextColor(Color.rgb(7, 200, 12));
				ibSubmit.setClickable(false);      //回答正确或错误,提交按钮不能被点击,防止一个一个尝试获取答案加分
				score+=10;
			} else {
				tvRightOrWrong.setText("啊偶,回答错了");
				tvRightOrWrong.setTextColor(Color.rgb(255, 00, 00));
				ibSubmit.setClickable(false);
			}
			if(score==60&&number<=10){
				tvRightOrWrong.setText("恭喜,闯关成功");
				tvRightOrWrong.setTextColor(Color.rgb(7, 200, 12));
				ibNext.setClickable(false);      //闯关成功或失败,下一道题的按钮不能被点击
			}else if(score<=60&&number>=10){
				tvRightOrWrong.setText("抱歉,闯关失败");
				tvRightOrWrong.setTextColor(Color.rgb(255, 00, 00));
				ibNext.setClickable(false);
			}
			break;

		case R.id.ibNext:   //生成下一道题
			clearPhrase();//清空TextView和RadioButton属性
			getNumber();    
			showPhrase();
			getFourPhrase();
			showRadiButton();
			number+=1;  //题目数目加一
			break;
		}
	}
	public void clearPhrase(){     //跳到下一道题,删除之前控件的属性
		tvRightOrWrong.setText(" ");      //清空提示语
		ibSubmit.setClickable(true);    //提交按钮可以点击
		rbPhraseOne.setChecked(false);    //单选按钮都不被选中
		rbPhraseTwo.setChecked(false);
		rbPhraseThree.setChecked(false);
		rbPhraseFour.setChecked(false);
	}
}


8.源码分享:点击打开链接


9.时间原因页面并没有美化,界面布局也有些问题,暂时只能提供简单学习,请谅解。












Android小程序-乐学成语游戏(四)

标签:

原文地址:http://blog.csdn.net/hester_hester/article/details/51671704

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!