Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created November 9, 2023 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/ea362f89d894556057e78f395dfbe444 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/ea362f89d894556057e78f395dfbe444 to your computer and use it in GitHub Desktop.
Android Quiz App Kotlin 7-4
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityResultBinding.inflate(layoutInflater)
setContentView(binding.root)
// 正解数を取得
val score = intent.getIntExtra("RIGHT_ANSWER_COUNT", 0)
// トータルスコアの読み出し
val prefs = getPreferences(Context.MODE_PRIVATE)
var totalScore = prefs.getInt("TOTAL_SCORE", 0)
// トータルスコアに今回のスコアを加算
totalScore += score
// TextViewに表示する
binding.resultLabel.text = getString(R.string.result_score, score)
binding.totalScoreLabel.text = getString(R.string.result_total_score, totalScore)
// トータルスコアを保存
val editor = prefs.edit()
editor.putInt("TOTAL_SCORE", totalScore)
editor.apply()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment