2015-12-30 44 views
-1

嗨,所以即時嘗試做一個遊戲,我想要高分保存。從我最喜歡的閱讀到使用的共享偏好。這裏是我的代碼:保存一個int並使用sharedpreferences在alertdialouge中顯示它Android

我在這裏宣佈整數

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); 
} 

}

+0

您可以使用相同的SharedPreferences對象來檢索特定的值。看看getInt()方法。 – scana

回答

1

該過程很簡單。 使用Sharedpreferences的步驟:

步驟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 

充分利用Sharedpreferences值 - 語法

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(); 

資源:

Android Developer Page

Tutorial 1

Tutorial 2

參見上述鏈接.. !!這將是很有幫助的。

這會幫助你.. !!試試吧......

更新回答

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(); 
    } 

} 
+0

我收到一個錯誤,說sharedPreferences不能應用於給定的類型; required:string,int found:string,string。我不知道這裏發生了什麼? – Pat

+0

你在哪一行出現這個錯誤?或者當你得到這個錯誤? –

+0

我使用這個SharedPreferences的任何一點sp = getSharedPreferences(filename,0); int value = data.getInt(「KEY VALUE」,「DEFAULT VALUE」);也是最好的地方,使用該行 – Pat

0
data = getSharedPreferences(filename, 0); 
if(data!=null){ 
int previous_highscore = data.getInt("Hscore"); 
} 
1

你可以有一個獨立的顯示高分課程爲:

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); 
    } 
} 
+0

您可以通過簡單地調用它來保存並獲取價值。 – priyanka

+0

對不起,我是新手,但是我打電話給getScore()類,因此它顯示alertdialoge中的當前高分 – Pat

+0

您必須爲此類創建對象。然後調用saveScore(yourScore)保存分數。然後致電getScore()獲得分數。 – priyanka

相關問題