如何在安卓设备内部保存数据?轮次计分应用存储需求咨询
解决方案:按轮次保存多组分数数据
1. 定义单轮分数的数据结构
用可序列化的类封装单轮的四组分数和轮次编号,方便直接写入文件:
import java.io.Serializable; public class RoundScores implements Serializable { private int team1Score; private int team2Score; private int team3Score; private int team4Score; private int roundNumber; public RoundScores(int roundNumber, int t1, int t2, int t3, int t4) { this.roundNumber = roundNumber; this.team1Score = t1; this.team2Score = t2; this.team3Score = t3; this.team4Score = t4; } // 生成getter方法用于读取数据 public int getTeam1Score() { return team1Score; } public int getTeam2Score() { return team2Score; } public int getTeam3Score() { return team3Score; } public int getTeam4Score() { return team4Score; } public int getRoundNumber() { return roundNumber; } }
2. 核心文件存储逻辑
封装存储工具类,用应用内部存储保存文件(无需额外权限),按轮次命名文件(如round_1.dat):
import android.content.Context; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ScoreStorage { private Context context; public ScoreStorage(Context context) { this.context = context; } // 保存单轮分数 public boolean saveRoundScores(RoundScores scores) { try { String fileName = "round_" + scores.getRoundNumber() + ".dat"; FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(scores); oos.close(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } // 读取指定轮次的分数 public RoundScores loadRoundScores(int roundNumber) { try { String fileName = "round_" + roundNumber + ".dat"; FileInputStream fis = context.openFileInput(fileName); ObjectInputStream ois = new ObjectInputStream(fis); RoundScores scores = (RoundScores) ois.readObject(); ois.close(); fis.close(); return scores; } catch (Exception e) { e.printStackTrace(); return null; } } // 获取所有已保存的轮次编号 public List<Integer> getSavedRounds() { List<Integer> rounds = new ArrayList<>(); String[] files = context.fileList(); for (String file : files) { if (file.startsWith("round_") && file.endsWith(".dat")) { String numStr = file.replace("round_", "").replace(".dat", ""); try { int round = Integer.parseInt(numStr); rounds.add(round); } catch (NumberFormatException e) { // 忽略无效命名的文件 } } } Collections.sort(rounds); return rounds; } }
3. 按钮交互与保存触发
在Activity中维护当前分数和轮次,绑定加减按钮更新分数,点击保存按钮触发存储:
import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class ScoreTrackerActivity extends AppCompatActivity { private int currentRound = 1; private int t1Score = 0, t2Score = 0, t3Score = 0, t4Score = 0; private ScoreStorage scoreStorage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_score_tracker); scoreStorage = new ScoreStorage(this); // 队伍1加分按钮 findViewById(R.id.btn_t1_add).setOnClickListener(v -> { t1Score++; updateScoreDisplay(R.id.tv_t1_score, t1Score); }); // 队伍1减分按钮 findViewById(R.id.btn_t1_minus).setOnClickListener(v -> { if (t1Score > 0) t1Score--; updateScoreDisplay(R.id.tv_t1_score, t1Score); }); // 同理实现队伍2、3、4的加减按钮... // 保存本轮按钮 findViewById(R.id.btn_save_round).setOnClickListener(v -> { RoundScores currentScores = new RoundScores(currentRound, t1Score, t2Score, t3Score, t4Score); boolean saved = scoreStorage.saveRoundScores(currentScores); if (saved) { Toast.makeText(this, "第" + currentRound + "轮已保存", Toast.LENGTH_SHORT).show(); // 重置分数,进入下一轮 resetScores(); currentRound++; } else { Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } }); } private void updateScoreDisplay(int textViewId, int score) { ((TextView) findViewById(textViewId)).setText(String.valueOf(score)); } private void resetScores() { t1Score = t2Score = t3Score = t4Score = 0; updateScoreDisplay(R.id.tv_t1_score, 0); updateScoreDisplay(R.id.tv_t2_score, 0); updateScoreDisplay(R.id.tv_t3_score, 0); updateScoreDisplay(R.id.tv_t4_score, 0); } }
4. 读取历史轮次数据
如果需要展示历史记录,可调用工具类的getSavedRounds获取所有已保存轮次,再逐个加载数据:
// 示例:加载并展示所有历史轮次 List<Integer> savedRounds = scoreStorage.getSavedRounds(); for (int round : savedRounds) { RoundScores scores = scoreStorage.loadRoundScores(round); if (scores != null) { String record = "第" + round + "轮:T1-" + scores.getTeam1Score() + " | T2-" + scores.getTeam2Score() + " | T3-" + scores.getTeam3Score() + " | T4-" + scores.getTeam4Score(); // 将record添加到列表控件展示 } }
注意事项
- 内部存储的文件仅当前应用可访问,无需申请
READ_EXTERNAL_STORAGE或WRITE_EXTERNAL_STORAGE权限 - 若需将文件保存到外部存储(如SD卡),需处理权限请求并修改存储路径
- 这种按轮次独立存文件的方式,方便后续单独删除或修改某一轮的数据
内容的提问来源于stack exchange,提问作者Speedy
相关产品推荐
相关产品推荐

