Java 使用共享首选项存储数据
在过去的3天里,我每天花6-8小时搜索、阅读和执行各种关于共享偏好的示例,但都没有成功。我走到了一条死胡同……非常令人沮丧!我的问题如下: -目标: 我现在正在做一个非常基本的数学游戏,玩家将得到一个(随机)方程,两个数字相乘。然后,玩家只需按下三个选项按钮中的一个(其中一个包括正确的答案),即可给出答案。每个正确答案都会增加分数 -问题: 现在,我想为游戏添加一个高分来存储最高分数。如何使用共享首选项或任何其他工作方法来存储数据 “Score:(@+id/textScore)是我的文本视图,其中包含分数(TESTActivity.java)。“Top Scores”(@+id/textTopScores)是我的文本视图,我想在其中存储我的高分(HighScoreActivity.java) -应用程序的结构:Java 使用共享首选项存储数据,java,android,Java,Android,在过去的3天里,我每天花6-8小时搜索、阅读和执行各种关于共享偏好的示例,但都没有成功。我走到了一条死胡同……非常令人沮丧!我的问题如下: -目标: 我现在正在做一个非常基本的数学游戏,玩家将得到一个(随机)方程,两个数字相乘。然后,玩家只需按下三个选项按钮中的一个(其中一个包括正确的答案),即可给出答案。每个正确答案都会增加分数 -问题: 现在,我想为游戏添加一个高分来存储最高分数。如何使用共享首选项或任何其他工作方法来存储数据 “Score:(@+id/textScore)是我的文本视图,其
My app looks like this:
(a) MainActivity - main page with a start button leading the player to the Menu (= MenuActivity)
(b) MenuActivity - here the player have 3 buttons options which includes:
Play Button --> leading to the TESTActivity
High Score button -->> leading to the HighScoreActivity
Help button -->> leading to the HelpActivity
活动测试.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_test"
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"
tools:context="com.example.android.laboration2.TESTActivity">
<TextView
android:id="@+id/textOperator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textPartA"
android:layout_centerHorizontal="true"
android:text="x"
android:textSize="55sp" />
<TextView
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textOperator"
android:layout_alignLeft="@+id/textOperator"
android:layout_alignStart="@+id/textOperator"
android:layout_marginTop="70dp"
android:id="@+id/textView"
android:textSize="70sp" />
<TextView
android:id="@+id/textPartA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonChoice1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/buttonChoice1"
android:layout_marginTop="36dp"
android:text="2"
android:textSize="60sp" />
<TextView
android:id="@+id/textPartB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="60sp"
android:layout_alignTop="@+id/textOperator"
android:layout_alignLeft="@+id/buttonChoice3"
android:layout_alignStart="@+id/buttonChoice3" />
<Button
android:id="@+id/buttonChoice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/buttonChoice1"
android:layout_centerHorizontal="true"
android:text="4"
android:textSize="30sp" />
<Button
android:id="@+id/buttonChoice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/buttonChoice2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="6"
android:textSize="30sp" />
<Button
android:id="@+id/buttonChoice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="70dp"
android:text="2"
android:textSize="30sp" />
<TextView
android:id="@+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/buttonChoice2"
android:layout_toStartOf="@+id/buttonChoice2"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="14dp" />
<TextView
android:id="@+id/textLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level:"
android:textSize="24sp"
android:layout_alignBaseline="@+id/textScore"
android:layout_alignBottom="@+id/textScore"
android:layout_alignRight="@+id/textPartB"
android:layout_alignEnd="@+id/textPartB" />
</RelativeLayout>
TESTActivity.java
package com.example.android.laboration2;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.Random;
公共类TESTActivity扩展了AppCompatActivity实现了View.OnClickListener{
按钮按钮对象选项1;
按钮按钮对象选择2;
按钮按钮对象选项3;
TextView textObjectPartA;
TextView textObjectPartB;
TextView textObjectScore;
TextView textObjectLevel;
TextView textObjectHighScoresCores;
正确答案;
int currentScore=0;
int currentLevel=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_测试);
textObjectPartA=(TextView)findViewById(R.id.textPartA);
textObjectPartB=(TextView)findViewById(R.id.textPartB);
textObjectHighScoresCores=(TextView)findViewById(R.id.textHighScoresCores);
textObjectScore=(TextView)findViewById(R.id.textScore);
textObjectLevel=(TextView)findViewById(R.id.textLevel);
buttonObjectChoice1=(按钮)findViewById(R.id.buttonChoice1);
buttonObjectChoice2=(按钮)findViewById(R.id.buttonChoice2);
buttonObjectChoice3=(按钮)findViewById(R.id.buttonChoice3);
buttonObjectChoice1.setOnClickListener(此选项);
buttonObjectChoice2.setOnClickListener(此选项);
buttonObjectChoice3.setOnClickListener(此选项);
//存储分数
String highScore=textObjectHighScoresCores.getText().toString();
int newHighScore=当前分数;
SharedReferences prefs=this.getSharedReferences(“StoringScore”,Context.MODE\u PRIVATE);
SharedReferences.Editor=prefs.edit();
编辑:putInt(highScore,newHighScore);
editor.apply();
设置问题();
}//onCreate在此结束
@凌驾
公共void onClick(视图){
//声明将在所有情况下使用的新int
int-answerGiven=0;
开关(view.getId()){
//使用buttonObjectChoice1,2,3的值初始化新int
案例R.id.buttonChoice1:
answerGiven=Integer.parseInt(“+buttonObjectChoice1.getText());
打破
案例R.id.buttonChoice2:
answerGiven=Integer.parseInt(“+buttonObjectChoice2.getText());
打破
案例R.id.buttonChoice3:
answerGiven=Integer.parseInt(“+buttonObjectChoice3.getText());
打破
}//交换机到此结束
更新CoreandLevel(回答已给出);
设置问题();
}//onClick到此结束
无效设置问题(){
//生成问题的各个部分
int numberRange=当前级别*3;
Random randInt=新的Random();
int partA=randInt.nextInt(numberRange);
partA++;//避免零值
int partB=randInt.nextInt(numberRange);
partB++;//避免零值
正确答案=A部分*B部分;
int错误答案1=正确答案-2;
int错误答案2=正确答案+2;
textObjectPartA.setText(“+partA”);
textObjectPartB.setText(“+partB”);
//为多选按钮提供一个介于0和2之间的数字
int buttonLayout=randInt.nextInt(3);
开关(按钮布局){
案例0:
按钮对象选择1.setText(“+correctAnswer”);
按钮对象选择2.setText(“+错误应答1”);
按钮对象选择3.setText(“+错误回答2”);
打破
案例1:
按钮对象选择2.setText(“+correctAnswer”);
按钮对象选择3.setText(“+错误回答1”);
按钮对象选择1.setText(“+错误回答2”);
打破
案例2:
按钮对象选择3.setText(“+correctAnswer”);
按钮对象选择1.setText(“+错误回答1”);
按钮对象选择2.setText(“+错误应答2”);
打破
}//交换机到此结束
}//问题到此结束
//更新玩家的分数和等级
无效更新CoreandLevel(给定整数){
如果(回答正确){
对于(inti=1;i来说,实际上主要问题就在这里。
//Storing the score
String highScore = textObjectHighScoresScores.getText().toString();
int newHighScore = currentScore;
SharedPreferences prefs = this.getSharedPreferences("StoringScore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(highScore, newHighScore);
editor.apply();
editor.putInt(key,value)
您使用高分作为键,该键将被更改,但共享首选项可以通过sam获得
SharedPreferences prefs = this.getSharedPreferences("StoringScore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("myKey", score);
editor.apply();
SharedPreferences prefs = this.getSharedPreferences("StoringScore", Context.MODE_PRIVATE);
int newHighscore = prefs.getInt("myKey", 0);
//I hope this will help you
public class TESTActivity extends AppCompatActivity implements View.OnClickListener{
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
TextView textObjectPartA;
TextView textObjectPartB;
TextView textObjectScore;
TextView textObjectLevel;
TextView textObjectHighScoresScores;
int correctAnswer;
int currentScore = 0;
int currentLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textObjectPartA = (TextView) findViewById(R.id.textPartA);
textObjectPartB = (TextView) findViewById(R.id.textPartB);
//textObjectHighScoresScores = (TextView) findViewById(R.id.textScore);
textObjectScore = (TextView) findViewById(R.id.textScore);
textObjectLevel = (TextView) findViewById(R.id.textLevel);
buttonObjectChoice1 = (Button) findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button) findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button) findViewById(R.id.buttonChoice3);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
setQuestion();
}//onCreate ends here
@Override
public void onClick(View view) {
//Declaring a new int which will be used in all the cases
int answerGiven = 0;
switch (view.getId()) {
//Initializes a new int with a value for buttonObjectChoice1,2,3
case R.id.buttonChoice1:
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
break;
case R.id.buttonChoice2:
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
break;
case R.id.buttonChoice3:
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
break;
}//switch ends here
updateScoreAndLevel(answerGiven);
setQuestion();
}//onClick ends here
void setQuestion() {
//Generates the parts of the question
int numberRange = currentLevel * 3;
Random randInt = new Random();
int partA = randInt.nextInt(numberRange);
partA++;//To avoid a zero value
int partB = randInt.nextInt(numberRange);
partB++;//To avoid a zero value
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 2;
int wrongAnswer2 = correctAnswer + 2;
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partB);
//Gives a number between 0 and 2 for the multi choice buttons
int buttonLayout = randInt.nextInt(3);
switch (buttonLayout) {
case 0:
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
break;
case 1:
buttonObjectChoice2.setText("" + correctAnswer);
buttonObjectChoice3.setText("" + wrongAnswer1);
buttonObjectChoice1.setText("" + wrongAnswer2);
break;
case 2:
buttonObjectChoice3.setText("" + correctAnswer);
buttonObjectChoice1.setText("" + wrongAnswer1);
buttonObjectChoice2.setText("" + wrongAnswer2);
break;
}//switch ends here
}//setQuestion ends here
//Updates the Score & Level for the player
void updateScoreAndLevel(int answerGiven){
if(isCorrect(answerGiven)){
for(int i = 1; i <= currentLevel; i++){
currentScore = currentScore + i;
}
currentLevel++;
}else{
currentScore = 0;
currentLevel = 1;
}
//Updates the Score & Level TextViews
textObjectScore.setText("Score: " + currentScore);
textObjectLevel.setText("Level: " + currentLevel);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
int storedHighScore = sharedPreferences.getInt("highScore",0);
if (currentScore>storedHighScore){
editor.putInt("highScore",currentScore);
editor.commit();
}
}//updateScoreAndLevel ends here
//Sends a toast message to the player - if right = Correct!, if wrong = Wrong! :(.
boolean isCorrect(int answerGiven){
boolean correctTrueOrFalse;
if(answerGiven == correctAnswer){
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_LONG).show();
correctTrueOrFalse=true;
}else{
Toast.makeText(getApplicationContext(), "Wrong! :(", Toast.LENGTH_LONG).show();
correctTrueOrFalse=false;
Intent intent = new Intent(this,HighScoreActivity.class);
startActivity(intent);
}
return correctTrueOrFalse;
}//isCorrect ends here
}//TESTActivity ends here
public class HighScoreActivity extends AppCompatActivity {
TextView highScoreTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences",MODE_PRIVATE);
int storedHighScore = sharedPreferences.getInt("highScore",0);
highScoreTextView=(TextView)findViewById(R.id.textHighScore);
highScoreTextView.setText(""+storedHighScore);
}
}