购买后如何减少用户硬币

时间:2020-04-23 06:48:33

标签: java android android-studio imagebutton

我需要帮助,每次成功单击imagebutton时,假设硬币足够,它将减少用户购买硬币的数量。您的帮助将不胜感激。谢谢。

MY SHOP IMAGE

    load30 = (ImageButton) findViewById(R.id.load30);
    load50 = (ImageButton) findViewById(R.id.load50);
    load100 = (ImageButton) findViewById(R.id.load100);
    load150 = (ImageButton) findViewById(R.id.load150);

    final SharedPreferences settings = getSharedPreferences("PREFS", Context.MODE_PRIVATE);

    coins = settings.getInt("COINS", 0);
    tv_coins.setText("" + coins);


    load30.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Shop.this, Thirty.class));
        }

    });

1 个答案:

答案 0 :(得分:0)

//define a global variable

//could be other than 5 (you choose)
private final int REDUCED_AMOUNT = 5;


load30.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//lets say you reduce 5 coins on each click

if(coins>REDUCED_AMOUNT){

//get reduced coins
int reducedCoins = coins - REDUCED_AMOUNT; 

//write to shared preferences the new coins
SharedPreferences.Editor editor = settings.edit();
editor.putInt("COINS" , reducedCoins);
editor.commit();

//start another activity
startActivity(new Intent(Shop.this, Thirty.class));

}else{

//you don't have enough coins to buy

}
}

});