2016-11-17 27 views
-1

我開發了一個答題器程序,當我點擊一個按鈕,然後分層計數點擊和15秒後按鈕鵝被禁用,並且我想要**我在15秒內點擊了多少,高分,當我越過那高分然後存儲在同一個活動**在我的應用程序中存儲數據(高分)

+1

....和問題是什麼? – Opiatefuchs

+0

^^你能詳細說明你想要什麼以及你嘗試過什麼嗎? –

+0

你是什麼意思? – user3476090

回答

0

我的得分新高秀我建議:

public class MainActivity extends AppCompatActivity { 

private int clicks = 0; 
private TextView mTextView; 
Button bt_restart; 
int high; 
public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String HIGHSCORE = "high" ; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); 
    high = prefs.getInt(HIGHSCORE, 0); 
    bt_restart = (Button)findViewById(R.id.restart); 
    bt_restart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent restartIntent = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage(getBaseContext().getPackageName()); 
      restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(restartIntent); 
     } 
    }); 

    mTextView = (TextView) findViewById(R.id.total_textview); 
    mTextView.setVisibility(View.VISIBLE); 

    Button button = (Button) findViewById(R.id.count_button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View v) { 
      final Button b = (Button)v; 
      if (clicks == 0){ 
       // Means its the first time that a user click the button 
       // Start a thread that is going to disable the button after 5 seconds from first click 
       new CountDownTimer(15000, 1000) { 
        public void onTick(long millisUntilFinished) { 
         b.setText(millisUntilFinished/1000 + " Seconds"); 
        } 

        public void onFinish() { 
         b.setText("Time up"); 
         b.setEnabled(false); 
         // Showing user clicks after button is disabled 
         showClicks(); 
        } 
       }.start(); 
      } 
      // Here we are just counting . . . . including the first click 
      countClicks(); 
     } 
    }); 

} 


private void countClicks(){ 
    ++clicks; 
    mTextView.setText(Integer.toString(clicks)); 

    // You can update your text view here 
} 
private void showClicks(){ 
    mTextView.setText(String.valueOf(clicks)+"Clicks"); 
    mTextView.setVisibility(View.VISIBLE); 
    if(clicks > high){ 
     SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
     editor.putInt(HIGHSCORE, clicks); 
     editor.commit(); 
     high = prefs.getInt(HIGHSCORE, 0); 
    }  
} 

然後,如果你要顯示的高分使用一個TextView和setText使用String.valueOf(高),我不知道當更改變量高時,textview是否會發生變化也許您必須編輯變量textview.setText everyTima高(代碼基於該鏈接:Android Shared preferences example

+0

」+1「我支持你的迴應它不工作來錯誤你可以請我做一個faver只是告訴GitHub何去何處我可以複製和過去 – user3476090

+0

什麼是錯誤? – dpart

+0

應用程序粉碎? – dpart

1

您可以使用sharedpreferences保存並檢查您的高分。就像這樣:

這些線下set_ContentView在開始:

String PREFS_GAME ="your package name"; 
SharedPreferences sp = getSharedPreferences(PREFS_GAME,Context.MODE_PRIVATE); 
final Integer oldrec = sp.getInt("record",0); 

然後編寫這些代碼setOnclicklistenre:

if (newrec>oldrec){ 
       sp.edit().putInt("record",new rec).commit(); 
       Toast.makeText(MainActivity.this,"your new record is :"+newrec, Toast.LENGTH_SHORT).show(); 
       } 
+0

,我必須把這個代碼 – user3476090

+0

放在第一「Oncreate」中有3行,當你想比較新的分數和以前的分數時加上「if」。 –

+0

你可以寫完整的代碼,我只是複製和害蟲。我是新的程序,請幫我 – user3476090

相關問題