我正在使用 Java 创建一个简单的 Click Counter Android 应用程序。我是 Java 新手。我正在尝试保存应用程序退出时的计数,无论是按下后退按钮还是应用程序关闭或崩溃等。这是我到目前为止的代码:
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
Button btnCount;
int count;
Button wmute;
Button wreset;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);
//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
count = app_preferences.getInt("count", 0);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", ++count);
editor.commit();
//Button SOUND AND COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");
}
我的应用程序有 2 个问题。
首先,计数没有保存到应用程序关闭然后再次打开时的状态。例如,如果计数为“20”并且我点击了后退按钮,则返回到同一页面,计数将显示为“3”。- 有趣的是,每次我尝试上述方法并在点击后退按钮后返回应用程序时,计数都会增加 1 个计数。
第二个问题是,当我返回应用程序并且计数显示为“5”时,重置按钮不再起作用 - 它什么也不做。但是当我继续计数然后点击重置按钮时,它再次将计数更改为零。
请任何人都可以协助解决上述2个问题?
建议后的新代码:
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
Button btnCount;
Button wmute;
Button wreset;
public static int count=0;
SharedPreferences app_preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);
//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
count = app_preferences.getInt("count", 0);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");}
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
//SOUND and COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");
}
我的代码的最后一部分:
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
};
});
}});
};}