我正在使用Java创建一个简单的Click Counter Android App.我是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");
}
Run Code Online (Sandbox Code Playgroud)
我在应用程序中遇到了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");
}
Run Code Online (Sandbox Code Playgroud)
我代码的最后一部分:
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
};
});
}});
};}
Run Code Online (Sandbox Code Playgroud)
在类级别声明count为static:
public static int count=0;
SharedPreferences app_preferences ;
Run Code Online (Sandbox Code Playgroud)
并onPause用于保存计数值 SharedPreferences:
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
555 次 |
| 最近记录: |