因此,当我点击保存按钮时,我正试图存储游戏分数,包括从我的篮球游戏计数器应用程序存储的球队名称和日期。我是编程的新手,我搜索过的所有内容都没有帮助我。
这是java代码:
package com.example.android.courtcounter;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0; int scoreTeamB = 0;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); displayForTeamA(0); displayForTeamB(0); setTitle("Basketball Count"); Toast.makeText(getApplicationContext(), "You can change team names and scores manually by clicking on them", Toast.LENGTH_LONG).show(); } // +3 Points for Team A public void addThreeForTeamA (View view) { scoreTeamA = scoreTeamA + 3; displayForTeamA(scoreTeamA); } // +2 Points for Team A public void addTwoForTeamA (View view) { scoreTeamA = scoreTeamA + 2; displayForTeamA(scoreTeamA); } // +1 Point for Team A public void addOneForTeamA (View view) { scoreTeamA = scoreTeamA +1; displayForTeamA(scoreTeamA); } // Displays the given score for Team A. public void displayForTeamA(int score) { TextView scoreView = (TextView) findViewById(R.id.team_a_score); scoreView.setText(String.valueOf(score)); } // +3 Points for Team B public void addThreeForTeamB (View view) { scoreTeamB = scoreTeamB + 3; displayForTeamB(scoreTeamB); } // +2 Points for Team B public void addTwoForTeamB (View view) { scoreTeamB = scoreTeamB + 2; displayForTeamB(scoreTeamB); } // +1 Point for Team B public void addOneForTeamB (View view) { scoreTeamB = scoreTeamB +1; displayForTeamB(scoreTeamB); } // Displays the given score for Team B. public void displayForTeamB(int score) { TextView scoreView = (TextView) findViewById(R.id.team_b_score); scoreView.setText(String.valueOf(score)); } public void resetScore (View view) { scoreTeamA = 0; scoreTeamB = 0; displayForTeamA(scoreTeamA); displayForTeamB(scoreTeamB); }
}
我已将分数和团队名称存储在EditText视图中,并希望保存它们并稍后加载它们。任何人都知道如何使用它来保存和加载特定的游戏分数?
由于
答案 0 :(得分:1)
我从Udacity的优秀“Android简介”课程中认识到这段代码。您必须使用sharedPreferences
来保存分数。例如:
SharedPreferences sharedPref = getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("scoreTeamA", scoreTeamA);
editor.putInt("scoreTeamB", scoreTeamB);
editor.apply();
然后检索值:
SharedPreferences sharedPref = getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int scoreTeamA = sharedPref.getInt(scoreTeamA, 0);
int scoreTeamB = sharedPref.getInt(scoreTeamB, 0);
答案 1 :(得分:0)
如果要为全世界的所有用户创建数据库,则应使用SQL和php编程语言。用于数据库编辑的SQL,用于控制数据库编辑的php。
但是,如果您只想为正在使用该应用程序的设备存储数据,那么您只需在android项目文件中创建一个文本文件即可。然后,您可以使用代码以您希望的方式填充文本文件。
答案 2 :(得分:0)
您可以使用数据库,但我认为它不仅仅是您需要的数据库。 您也可以使用文件:
在manifest.xml中添加以下内容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
添加两个功能:
void writeToFile(String string) {
FileOutputStream outputStream = openFileOutput("thiswillbethenameofyourfile", Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
}
String readFromFile() {
String content;
FileInputStream inputStream = openFileInput("thiswillbethenameofyourfile");
byte[] buffer = new byte[1024];
int n;
while ((n = inputStream.read(buffer)) != -1)
{
content.append(new String(buffer, 0, n));
}
return content;
}
使用包含分数的字符串调用WriteToFile。
同样@Andrew Sun提到你可以使用sharedPreferences
https://developer.android.com/training/basics/data-storage/shared-preferences.html