2016-08-15 56 views
0

我目前正在學習android應用程序開發和開始階段。我製作了這款遊戲,現在我想在我的屏幕上設置商店並以Textview的形式顯示高分。如果有人能幫助我,我真的很感激。謝謝!存儲最高分並顯示它

package com.princeghimire.clickmeter; 

    import android.content.DialogInterface; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.os.CountDownTimer; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 


    public class MainActivity extends AppCompatActivity{ 

    TextView tv_time, tv_clicks; 
    Button b_start, b_click; 

    CountDownTimer timer; 
    int time = 10; 
    int clicks = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setLogo(R.drawable.iconnn); 
     getSupportActionBar().setDisplayUseLogoEnabled(true); 
     tv_time = (TextView) findViewById(R.id.tv_time); 
     tv_clicks = (TextView) findViewById(R.id.tv_clicks); 
     b_start = (Button) findViewById(R.id.b_start); 
     b_click = (Button) findViewById(R.id.b_click); 

     b_start.setEnabled(true); 
     b_click.setEnabled(false); 

     timer = new CountDownTimer(10000, 1000) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
       time--; 
       tv_time.setText("Time: " + time); 
      } 

      @Override 
      public void onFinish() { 
       b_start.setEnabled(true); 
       b_click.setEnabled(false); 
       tv_time.setText("Time: 10"); 
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
       builder.setTitle("Time's Up Buddy!"); 
       builder.setMessage("Your Score Is: " + clicks); 
       builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int which) { 
         dialogInterface.dismiss(); 
         // finish(); 
        } 
       }); 
       AlertDialog alertDialog = builder.create(); 
       alertDialog.show(); 
      } 
     }; 

     b_click.setOnClickListener(new View.OnClickListener(){ 
      public void onClick (View v){ 
       clicks++; 
       // switchh.start(); 
       tv_clicks.setText("Your Clicks: " + clicks); 
      } 
     }); 

     b_start.setOnClickListener(new View.OnClickListener(){ 
      public void onClick (View v){ 
       timer.start(); 
       b_start.setEnabled(false); 
       b_click.setEnabled(true); 
       clicks = 0; 
       time = 10; 
       tv_time.setText("Time: " + time); 
       tv_clicks.setText("Your Clicks: " + clicks); 
      } 
     }); 


    } 
    public void onBackPressed() 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure you want to EXIT?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         finish(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
} 
+1

只需使用'SharedPreferences'勾選這個des。 https://developer.android.com/reference/android/content/SharedPreferences.html –

+0

檢查此代碼,它可能會幫助您瞭解http://stackoverflow.com/a/37924565/5723796 –

回答

0

基本上是這樣的過程:

  1. 創建SharedPreferences的實例,並SharedPreferences.Edit SharedPreferences sharedPref = .... AND SharedPreferences.Edit editor = .....
  2. 呼叫這在你的遊戲結束if (sharedPref.getInt("highscore",0) < clicks) { editor.putInt("highscore",clicks).apply() }

上面的代碼將第一檢查您保存在「高分」鍵中的最後一個分數,如果您當前的點擊量較高,則會用它替換它,並且如果這是第一場比賽,所以默認值將被視爲0,所以第一場比賽的比分將被保存爲高比分,除非它再次爲0。

我已經做了幾乎相同的遊戲,它的Taplay和PlayStore btw。

祝你好運。

0

您可以使用SharedPreferences,如果您想跟蹤用戶遊戲統計信息,建議您使用Sqlite