我想保存一个值(" Coins"),并且我使用SharedPreferences选项(我希望即使关闭应用后也会保存硬币数量)。 我使用了2个函数,一个用于插入(/更改)硬币编号,另一个用于获取硬币编号。另外,我做了一个按钮,点击它后,它改变了硬币的数量。问题是,按钮没有改变它。有帮助吗?提前谢谢。
package com.myfirstapplication.owner.appversion1;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnPlay;
public void changeCoinsNumber(String key, int value) {
SharedPreferences sharedPreferences = getSharedPreferences("PlayerInfo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public int loadCoinsNumber(String key) {
SharedPreferences sharedPreferences = getSharedPreferences("PlayerInfo", Activity.MODE_PRIVATE);
int savedValue = sharedPreferences.getInt(key, 0);
return savedValue;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeCoinsNumber("CoinsNumber", 5);
TextView txt = (TextView) findViewById(R.id.spText);
txt.setText(""+loadCoinsNumber("CoinsNumber")); // displaying the number of coins
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeCoinsNumber("CoinsNumber", 111);
}
});
}
}
答案 0 :(得分:0)
如果您根据代码更改硬币值,则需要再次显示它。我正在更新你的一小部分代码。
var crypto = require('crypto');
var secretKey = 'change-me';
var loginUrl = 'http://example.com/login'
// called to work out where to redirect to for login
function getLoginUrl(redirectBackUrl) {
var sig = crypto.createHmac('sha1', secretKey)
.update(redirectBackUrl)
.digest('hex');
return loginUrl
+ '?redirect='
+ encodeURIComponent(redirectBackUrl)
+'&sig=' + sig;
}
// called by the login function to ensure that the
// redirect parameter is valid
function isRedirectUrlValid(url, sig) {
var expectedSig
= crypto.createHmac('sha1', secretKey)
.update(url)
.digest('hex');
return expectedSig === sig;
}
希望它会对你有所帮助:)。