Java 使用SharedReferences Android保存int并在alertdialouge中显示
嗨,我想做一场比赛,我想把最高分保存下来。从我所读到的资料来看,最好使用SharedReference。这是我的密码: 我在这里申报Java 使用SharedReferences Android保存int并在alertdialouge中显示,java,android,sharedpreferences,android-sharedpreferences,Java,Android,Sharedpreferences,Android Sharedpreferences,嗨,我想做一场比赛,我想把最高分保存下来。从我所读到的资料来看,最好使用SharedReference。这是我的密码: 我在这里申报 public int score; public int highScore; SharedPreferences data; public static String filename = "HighScore"; 然后我把它叫做“创造” @Override protected void onCreate(Bundle savedInstanceState)
public int score;
public int highScore;
SharedPreferences data;
public static String filename = "HighScore";
然后我把它叫做“创造”
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit();
}
现在我想在alertdialouge中显示最高分
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setTitle("You have lost");
myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is :" + \\read highscore and display here)
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
score=0;
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
String points = String.valueOf(score);
myScore.setText(points);
}
})
.create();
谢谢你的帮助
public class MainActivity extends ActionBarActivity {
public int score;
public int highScore = 10;
SharedPreferences data;
public static String filename = "HighScore"; // This is shared preference name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getSharedPreferences(filename, 0);
/*SharedPreferences.Editor editor = data.edit();
editor.putInt("HighScore", highScore);
editor.apply(); // Use editor.apply() for saving in background*/
SharedPreferences data = getSharedPreferences(filename, 0);
int currentscore;
currentscore = 10;
highScore = data.getInt("Hscore", 0); // lets say highscore = 100
if(highScore>currentscore)
{
// This will store the new high score in the sharedpreferences.
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit(); // Use editor.apply() for saving in background
// after this highscore will be 100
}
}
public void generateH(View v){
Random rand = new Random();
int number = rand.nextInt(2)+1;
TextView myText = (TextView)findViewById(R.id.coinResult);
if (number == 1){
myText.setText("HEADS");
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
score = score+1;
String points = String.valueOf(score);
myScore.setText(points);
}
else{
myText.setText("TAILS");
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setTitle("You have lost");
myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is: " + data.getInt("Hscore", 0) )
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
score = 0;
TextView myScore = (TextView) findViewById(R.id.scoreTxt);
String points = String.valueOf(score);
myScore.setText(points);
myAlert.show();
}
}
public void generateT(View v){
Random rand = new Random();
int number = rand.nextInt(2)+1;
TextView myText = (TextView)findViewById(R.id.coinResult);
if(score > highScore){
highScore = score;
}
if (number == 1){
myText.setText("HEADS");
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setTitle("You have lost");
myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is :" + data.getInt("Hscore", 0))
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
})
.create();
score = 0;
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
String points = String.valueOf(score);
myScore.setText(points);
myAlert.show();
}
else{
myText.setText("TAILS");
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
score = score+1;
String points = String.valueOf(score);
myScore.setText(points);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}您可以将一个单独的类设置为:
public class ScoreSharedPreference {
private static final String PREFS_NAME = "SCORE_PREFS_";
private static final String CURRENT_SCORE = "CURRENT_SCORE";
SharedPreferences prefs;
Context context;
public ScoreSharedPreference(Context context) {
this.context=context;
prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
public void saveScore(int score) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(CURRENT_SCORE, score);
editor.commit();
}
public int getScore() {
return prefs.getInt(CURRENT_SCORE, 0);
}
}
这个过程很简单。
使用SharedReference的步骤:
步骤1:您需要创建一个共享首选项变量来保存高分
步骤2:需要将当前高分保存到共享偏好变量中
步骤3:在需要时检索高分
请尝试以下代码:
public int score;
public int highScore;
SharedPreferences data;
public static String filename = "HighScore"; // This is shared preference name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化共享首选项
data = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit(); // Use editor.apply() for saving in background
} // on create ends
//这是在共享首选项中插入/存储高分值的位置
data = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit(); // Use editor.apply() for saving in background
} // on create ends
从SharedReferences获取值-语法
SharedPreferences sp = getSharedPreferences(filename, 0);
int value = data.getInt("KEY VALUE", "DEFAULT VALUE"); // If there is no shared preference defined for the given key value default value is returned.
在警报对话框中显示高分
SharedPreferences data = getSharedPreferences(filename, 0);
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setTitle("You have lost");
myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is :" + data.getInt("Hscore", 0)) // refer syntax
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
score=0;
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
String points = String.valueOf(score);
myScore.setText(points);
}
})
.create();
资源:
请参考以上链接。。!!这将非常有帮助
这将对你有帮助。。!!试试看
更新的答案
public void generateH(View v){
Random rand = new Random();
int number = rand.nextInt(2)+1;
TextView myText = (TextView)findViewById(R.id.coinResult);
if (number == 1){
myText.setText("HEADS");
TextView myScore = (TextView)findViewById(R.id.scoreTxt);
score = score+1;
String points = String.valueOf(score);
myScore.setText(points);
if(highScore>points)
{
// This will store the new high score in the sharedpreferences.
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit(); // Use editor.apply() for saving in background
// after this highscore will be 100
}else
{
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", points);
editor.commit();
}
}
else{
myText.setText("TAILS");
score = 0;
TextView myScore = (TextView) findViewById(R.id.scoreTxt);
String points = String.valueOf(score);
myScore.setText(points);
if(highScore>points)
{
// This will store the new high score in the sharedpreferences.
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", highScore);
editor.commit(); // Use editor.apply() for saving in background
// after this highscore will be 100
}else
{
SharedPreferences.Editor editor = data.edit();
editor.putInt("Hscore", points);
editor.commit();
}
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setTitle("You have lost");
myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is: " + data.getInt("Hscore", 0) )
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
myAlert.show();
}
}
您可以使用相同的SharedReferences对象来检索特定值。看看getInt()方法。您可以通过简单地调用它来保存和获取值。很抱歉,我是新手,但我是否调用getScore()类,以便它在AlertDialogy中显示当前的高分?您必须为此类创建对象。然后致电saveScore(yourScore)获取保存分数。然后调用getScore()来获取分数。因此我尝试在if语句中调用saveScore(yourScore):if(score>yourScore){score=yourScore;saveScore(yourScore);};但这给了我一个错误。我不擅长保存数据,所以我有很多麻烦。我还将你的分数作为一个公共的分数,我得到一个错误,说SharedReferences不能应用于给定的类型;必需:字符串,找到int:字符串,字符串。我不确定这里发生了什么?你在哪一行得到这个错误?或者当您遇到此错误时?我使用此SharedReferences sp=GetSharedReferences(文件名,0)的任意点;int value=data.getInt(“键值”、“默认值”);还有,哪里是使用这条线的最佳地点哦。。酷毙了,把“默认值”替换成任何默认值,比如0。因为您使用int作为返回类型。添加了一些有用的参考链接。。转介他们