- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在过去的 3 天里,我实际上每天花费 6-8 小时来搜索、阅读和执行有关共享首选项的各种示例,但没有任何成功。我走到了死胡同……非常令人沮丧!我的问题如下:
- 目标:我目前正在做一个非常基础的数学游戏,玩家将得到一个(随机)方程式,其中两个数字相互相乘。然后玩家将通过简单地按下三个选项按钮之一(一个包括正确答案)来给出答案。每个正确答案都会加分。
- 问题:现在,我想为游戏添加一个高分来存储最高分。我如何使用共享首选项或任何其他工作方法来存储我的数据。
"Score:"(@+id/textScore) 是我的包含分数 (TESTActivity.java) 的 TextView。 “Top Scores”(@+id/textTopScores) 是我的 TextView,我想在其中存储我的高分 (HighScoreActivity.java)。
- 应用的结构:
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
.
activity_test.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;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
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_test);
textObjectPartA = (TextView) findViewById(R.id.textPartA);
textObjectPartB = (TextView) findViewById(R.id.textPartB);
textObjectHighScoresScores = (TextView) findViewById(R.id.textHighScoresScores);
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);
//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();
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);
}//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;
}
return correctTrueOrFalse;
}//isCorrect ends here
}//TESTActivity ends here
activity_high_score.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_high_score"
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"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.laboration2.HighScoreActivity">
<TextView
android:text="@string/high_scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:id="@+id/textHighScore"
android:textSize="30sp"/>
<TextView
android:text="@string/top_scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textTopScores"
android:textSize="16sp"
android:layout_below="@+id/textHighScore"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp" />
</RelativeLayout>
HighScoreActivity.java
package com.example.android.laboration2;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HighScoreActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("StoringScore", Context.MODE_PRIVATE);
int newHighscore = prefs.getInt(highScore, null); //0 is the default value
}//onCreate ends here
}//HighScoreActivity ends here
最佳答案
其实主要问题就在这里
//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)
您正在使用高分作为键,这将被更改,但共享偏好可以通过存储时使用的相同键获得。
你应该这样存储
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);
Main point is KEY should be same
关于java - 使用共享首选项存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599231/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!