我需要帮助,每次成功单击imagebutton时,假设硬币足够,它将减少用户购买硬币的数量。您的帮助将不胜感激。谢谢。
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));
}
});
答案 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
}
}
});