我開發了一個答題器程序,當我點擊一個按鈕,然後分層計數點擊和15秒後按鈕鵝被禁用,並且我想要**我在15秒內點擊了多少,高分,當我越過那高分然後存儲在同一個活動**在我的應用程序中存儲數據(高分)
回答
我的得分新高秀我建議:
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)
」+1「我支持你的迴應它不工作來錯誤你可以請我做一個faver只是告訴GitHub何去何處我可以複製和過去 – user3476090
什麼是錯誤? – dpart
應用程序粉碎? – dpart
您可以使用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();
}
,我必須把這個代碼 – user3476090
放在第一「Oncreate」中有3行,當你想比較新的分數和以前的分數時加上「if」。 –
你可以寫完整的代碼,我只是複製和害蟲。我是新的程序,請幫我 – user3476090
- 1. 我的Android應用程序不存儲高分
- 2. 在我的應用程序中將數據存儲到xml中
- 3. 我應該如何備份我的應用程序存儲在GAE數據存儲區中的數據?
- 4. 在Android應用程序中存儲高分
- 5. 我應該在哪裏存儲C#應用程序的數據?
- 6. 在iOS應用程序中,我在哪裏存儲保存的用戶數據?
- 7. Python存儲數據,高分
- 8. 使用Shiny應用程序高效地存儲數據
- 9. iPhone應用程序的數據存儲
- 10. 存儲或不存儲Facebook應用程序中的數據
- 11. 在哪裏存儲我的android應用程序數據庫?
- 12. 我的應用程序數據庫存儲在哪裏
- 13. 將數據存儲在Ionic應用程序的緩存中
- 14. Stateful Rails應用程序。不在數據庫中存儲數據
- 15. 將小應用程序遷移到高複製數據存儲
- 16. 從Android應用程序將數據存儲在App Engine數據存儲中
- 17. 在Android應用程序中存儲對象的最佳高效數據結構
- 18. 我如何在Groovy應用程序中存儲數據作爲(會話,閃存)在Web應用程序中
- 19. 從C#應用程序存儲數據
- 20. facebook應用程序存儲數據(mp3)
- 21. 爲python應用程序存儲數據
- 22. Facebook應用程序數據存儲
- 23. 存儲應用程序敏感數據
- 24. 爲iPhone應用程序存儲數據
- 25. 我應該在哪裏存儲我的iPhone應用程序的SQLite數據庫?
- 26. 我應該將參考數據存儲在我的應用程序內存或數據庫中嗎?
- 27. 存儲數據與我的iPhone應用程序使用
- 28. 我想通過我們的應用程序在「EverNote」中存儲一些數據
- 29. 我應該在哪裏將靜態數據存儲在Web應用程序中
- 30. 如何在iOS應用程序中遠程存儲數據?
....和問題是什麼? – Opiatefuchs
^^你能詳細說明你想要什麼以及你嘗試過什麼嗎? –
你是什麼意思? – user3476090