如何将用户币添加到 Kotlin 中的现有币中?

How to add user coins to existing coins in Kotlin?

我正在用 Kotlin 开发游戏。我的目标是每次用户按下特定按钮时,他都会在现有硬币中添加 20 个硬币。之后,应该使用共享首选项保存硬币数量。

共享首选项:

val sharedPreferences = getSharedPreferences("SP_INFO", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
var coins = preferenceFile.getInt("COINS", 0)

下面是显示硬币数量的文本视图的布局:

//Coin Label
<TextView android:id="@+id/coinlabel"
          android:layout_width="150dp"
          android:layout_height="60dp"

          android:textSize="40sp"
          android:textColor="@color/coinlabel"
          android:textAlignment="textStart"

          android:layout_marginTop="20dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          />

我尝试将 20 个硬币添加到现有硬币中,但它并没有像我想要的那样工作。

var coins = Int

button.setOnClickListener {

        coins = 20 + coins  //The plus symbol may be used wrong here
        editor.putInt("COINS",coins)
        editor.apply()  //Here I want to save the coins number to shared-preferences
        coinlabel.setText(Integer.toString(coins))

        println(coins)
    }

整个class:

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import kotlinx.android.synthetic.main.activity_main.*
import android.animation.ObjectAnimator
import android.animation.AnimatorSet
import android.content.Intent
import android.net.Uri
import android.view.MotionEvent
import android.content.Context
import android.media.MediaPlayer


class MainActivity : AppCompatActivity() {


val sharedPreferences = getSharedPreferences("SP_INFO", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
var coins = sharedPreferences.getInt("COINS", 0)

var pressed: Boolean? = false
var audioon = true


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    supportActionBar?.hide()
    this.getWindow()
        .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)


  soundon.setOnClickListener{

        audioon = false

        editor.putBoolean("AUDIOON", audioon)
        editor.apply()
     }

    soundoff.setOnClickListener {

        audioon = true

        editor.putBoolean("AUDIOON", audioon)
        editor.apply()
    }

  button.setOnClickListener {

        coins += 20
        coinlabel.setText(Integer.toString(coins))
        editor.putInt("COINS",coins)
        editor.apply()
        println(coins)
    }
   }
  }

不能在Activity生命周期函数之外初始化这些依赖于Context的参数,因为在Activity class首次实例化时,Context并不完全可用:

val sharedPreferences = getSharedPreferences("SP_INFO", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
var coins = sharedPreferences.getInt("COINS", 0)

相反,您可以将它们设为 var lateinit 并在 onCreate 中实例化它们,如下所示:

lateinit var sharedPreferences: SharedPreferences
lateinit var editor: SharedPreferences.Editor
var coins = 0

//...

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    sharedPreferences = getSharedPreferences("SP_INFO", Context.MODE_PRIVATE)
    editor = sharedPreferences.edit()
    coins = sharedPreferences.getInt("COINS", 0)
    //...
}