有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为篮球计分器应用程序存储包括球队名称在内的比赛分数?

我试图在点击保存按钮时,从我的篮球比赛计数器应用程序中存储比赛分数,包括球队名称和日期

以下是Java代码:

package com.example.安卓.courtcounter;

import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.TextView;
import 安卓.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视图中,希望保存并稍后加载。有人知道如何保存和加载特定的游戏分数吗


共 (3) 个答案

  1. # 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);
    
  2. # 2 楼答案

    你可以使用数据库,但我认为这超出了你的需要。 您还可以使用以下文件:

    1. 将这些添加到您的清单中。xml:

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      
    2. 增加两个功能:

      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;
      } 
      
    3. 使用包含分数的字符串调用WriteToFile

    4. 调用readFromFile以获取该分数
    5. 如果您应该将所有内容都存储在一个文件中,那么您可以使用多个文件来存储多个数据,但这是一个好的开始

    正如@Andrew Sun提到的,您可以使用SharedReferences

    https://developer.android.com/training/basics/data-storage/shared-preferences.html

  3. # 3 楼答案

    如果您想为世界各地的所有用户创建一个数据库,那么您应该使用SQL和php编程语言。SQL用于数据库编辑,php用于控制数据库编辑

    但是,如果您只想存储您正在使用应用程序的设备的数据,那么您只需在android项目文件中创建一个文本文件即可。然后,您可以使用代码以您想要的方式填充文本文件