如何保存在一个谜语游戏的答案,而无需创建一个数据库? [英] How to save an answer in a riddle game without creating a database?

查看:159
本文介绍了如何保存在一个谜语游戏的答案,而无需创建一个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一问一答的游戏不同的级别,每个级别由一个问题。我没有与数据库中创建它。我只是用字符串。当用户回答了一个层次的问题,他是采取二级但是当用户返回到一个级别,他要再次键入答案即使他之前解决它。反正在JAVA保持在类型面板的回答(如果用户的解决它),而无需创建一个数据库?另外,虽然在式面板输入,用户必须删除键入此处...,然后回答。有反正,当用户点击输入​​键入这里......被自动删除?

I've created a question and answer game with different levels, each level consisting a question. I didn't create it with a database. I just used string. When the user answers a question in level one he is taken to level two but when the user returns back to level one, he has to type the answer again even though he's solved it before. Is there anyway in JAVA to keep the answer in the type panel (if the user's solved it) without having to create a database?? Also, while typing in the type panel, the user has to delete the "Type here..." and then answer. Is there anyway that when user taps to type the "Type here..." is automatically erased?

下面是我的一级activity.xml:

Here's my level one activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:weightSum="1"
    android:textAlignment="center"
    android:id="@+id/level1">

    <TextView
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:text="What has 88 keys but cannot open a single door?"
        android:id="@+id/que1"
        android:width="255dp"
        android:textSize="30dp"
        android:layout_margin="50dp"
        android:textStyle="italic"
        android:gravity="center" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/type1"
        android:layout_gravity="center_horizontal"
        android:text="Type here..." />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check answer..."
        android:id="@+id/check1"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

这是我的Oneactivity.java

and here's my Oneactivity.java

package com.golo.user.gaunkhanekatha;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Handler;



public class OneActivity extends Activity {
public SharedPreferences preferences; //ADDED THIS LINE
    public Button check;
    public EditText typeh;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        toast = Toast.makeText(OneActivity.this, "", Toast.LENGTH_SHORT);
        check = (Button)findViewById(R.id.check1); //R.id.button is the id on your xml
        typeh = (EditText)findViewById(R.id.type1); //this is the EditText id
        check.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                //Here you must get the text on your EditText
                String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
                //You can make an if statement to check if it's correct or not
                if(Answer.equals("Piano") || (Answer.equals("Keyboard")))
                {
                    preferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("Level 1 question 1 ", 1); //Depends of the level he have passed.
                editor.apply();
                ///Correct Toast
                toast.setText("Correct");
                toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
                toast.show();
                Intent i = new Intent(OneActivity.this, TwoActivity.class);
                startActivity(i);
                finish();

                }
                else{
                    //It's not the correct answer
                    toast.setText("Wrong! Try again...");
                    toast.show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(toast!= null) {
            toast.cancel();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_aboutus, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

此外,显示敬酒那里的键盘。反正是有敬酒屏幕移动到某个地方在屏幕上它是清晰可见的?

Also, the toast is displayed where there's keyboard. Is there anyway to move the toast screen to somewhere on the screen where it is clearly visible?

推荐答案

我给你两个方法做到这一点:

I'll give to you two methods to do it :

创建一个 LevelEarned 类,如下所示:

Create a LevelEarned class as follows :

public class LevelEarned {
 public static int level = 0;
}

每当你得到一个意图(因为用户已经正确地回答这个问题),只需键入:

Everytime you get an Intent (because user has answered the question correctly) just type :

LevelEarned.level = 1; // 1 depends with the level you have answered correctly

和它的,我会用这个这就是所谓的 共享preferences

And the best method that it's that I'd use to this it's called SharedPreferences

您可以使用共享preferences保存任何基本数据:布尔型,浮点型,整型,长整型和字符串。该数据将在用户会话持久(即使你的应用程序被杀害)。

You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

你必须做的第一件事是商店的共享preferences这个数据,你用编辑如下:

The first thing you have to do is store this data on SharedPreferences and you do it with an Editor as follows :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("player_level",1); //Depends of the level he have passed.
editor.apply();

注意

我想,当用户接受的问题,所以你会马上做,你会做一个按钮单击所以你必须通过一个背景 v.getContext()如果你是不是在 ButtonClick 键,你对你的的onCreate ()就叫这个来参考您的背景

NOTE

I guess you will do it immediately when the user accepts the question so, you'll do it on a Button click so you'll have to pass as a context v.getContext() if you are not on a ButtonClick and you are on your onCreate() just call this to refer your context.

要获得存储的数据(级别),你需要做到这一点:

To get the stored data (level) you'll need to do this :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int level = preferences.getInt("player_level", 0);

让我们给您解释一下。

Let's explain to you a little bit.

第一个参数是要找到关键的共享preference 所以它不会改变,第二个是一个默认值,它的的情况下,用它没有找到任何PLAYER_LEVEL 共享preferences

The first parameter it's the key to find the SharedPreference so it won't change and the second one is a default value that it's used in case it doesn't find any "player_level" SharedPreferences.

希望它可以帮助你保持在code打算:)

Hope it helps to you to keep going in your code :)

创建共享preferences preferences 作为一个全局变量如下:

Create SharedPreferences preferences as a global variable as follows :

public SharedPreferences preferences;

然后里面的的onClick()方法添加这些车道:

check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            //Here you must get the text on your EditText
            String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
            //You can make an if statement to check if it's correct or not
            if(Answer.equals("4") || (Answer.equals("four")))
            {
                preferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("player_level",1); //Depends of the level he have passed.
                editor.apply();
                //Create a Toast because it's correct
                Toast.makeText(OneActivity.this, "Correct!",
                        Toast.LENGTH_LONG).show();
            }
            else{
                //It's not the correct answer
                Toast.makeText(OneActivity.this, "Wrong! Try Again",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

和,你想知道你的水平只有将不得不做的事:

And where you want to know the level you only will have to do :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int level = preferences.getInt("player_level", 0); //level is the current level of the player.

编辑3

创建一个类如下:

EDIT 3

Create a class as follows :

 public static class LevelPlayer(){

     public int static getLevelPlayer(){

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     int level = preferences.getInt("player_level", 0); //level is the current level of the playe


     return level;
     }

  }

和你要问你做玩家的等级每次:

And every time you want to ask the level of the player you do :

int Level = LevelPlayer.getLevelPlayer(); //that's the level

所以,每一个问题后,你可以问水平,并把下一个问题。

So after every question you can ask the level and put the next question.

我已经做了一些修改,删除您拉特类,并把这个code:

I've made some changes, delete your lvl class and put this code :

public class lvl{
public Context mcontext;

public lvl(Context context){
    this.mcontext = context;

}
public int getLevelPlayer(){

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mcontext);
    int level = preferences.getInt("player_level", 0); //level is the current level of the playe
    return level;
}

然后在你的 MainActivity (或任何你要知道什么是玩家的级​​别),你必须把这个:

Then on your MainActivity(or wherever you have to know what's the level of the player) you have to put this :

public levelplayer mlevelplayer;

的onCreate()将此然后里面:

mlevelplayer = new levelplayer(this);
int level  = mlevelplayer.getLevelPlayer();

这篇关于如何保存在一个谜语游戏的答案,而无需创建一个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆