我创建了一个不同级别的问答游戏,每个级别包含一个问题。我没有用数据库创建它。我只是用字符串。当用户在第一级回答问题时,他会被带到第二级,但当用户返回第一级时,他必须再次输入答案,即使他之前已经解决了。 JAVA 中有没有在不必创建数据库的情况下将答案保留在类型面板中(如果用户已经解决了)?此外,在类型面板中键入时,用户必须删除“在此处键入...”然后回答。无论如何,当用户点击键入时,“在此处键入...”是否会自动删除?
这是我的一级 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
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);
}
}
此外, toast 显示在有键盘的地方。有没有办法将 toast 屏幕移动到屏幕上清晰可见的某个位置?
最佳答案
我会给你两种方法来做到这一点:
创建一个 LevelEarned
类如下:
public class LevelEarned {
public static int level = 0;
}
每次你得到一个Intent
(因为用户已经正确回答了问题)只需输入:
LevelEarned.level = 1; // 1 depends with the level you have answered correctly
我为此使用的最好方法叫做 SharedPreferences
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).
您要做的第一件事是将此数据存储在 SharedPreferences
上,然后使用 Editor
执行此操作,如下所示:
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();
注意
我猜你会在用户接受问题时立即执行此操作,因此,你将在单击 Button
时执行此操作,因此你必须将 v.getContext 作为上下文传递()
如果你不在 ButtonClick
而你在你的 onCreate()
只需调用 this
来引用你的 上下文
。
要获取存储的数据(级别),您需要执行以下操作:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int level = preferences.getInt("player_level", 0);
让我们稍微解释一下。
第一个参数是找到 SharedPreference
的关键,所以它不会改变,第二个参数是默认值,在找不到任何 "player_level 时使用"
SharedPreferences
。
希望它能帮助您继续编写代码:)
编辑2
创建 SharedPreferences preferences
作为全局变量,如下所示:
public SharedPreferences preferences;
然后在您的 onClick()
方法中添加这些 channel :
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();
}
}
});
而你想知道你只需要做的水平的地方:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int level = preferences.getInt("player_level", 0); //level is the current level of the player.
编辑 3
创建一个类如下:
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;
}
}
每次你想问你的玩家水平时:
int Level = LevelPlayer.getLevelPlayer(); //that's the level
所以在每个问题之后你可以问级别并提出下一个问题。
编辑4
我做了一些更改,删除了你的 lvl 类并放置了这段代码:
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
(或者任何你必须知道玩家等级的地方)你必须把这个:
public levelplayer mlevelplayer;
然后在你的 onCreate()
中添加:
mlevelplayer = new levelplayer(this);
int level = mlevelplayer.getLevelPlayer();
关于java - 如何在不创建数据库的情况下保存谜语游戏中的答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32324876/