如何保存按钮的点击次数?

时间:2015-01-05 00:55:05

标签: java android

基本上用户安装应用程序,首次进入并点击按钮[例如] 37次 - >现在这个号码必须保存为新记录,因为37大于0.第二次用户点击并进行[例如] 88次点击。现在我想保存88,并且可以通过将其放入PopupDialog中随时查看它。我从哪里开始?

这是我的代码:

public class MainActivity extends ActionBarActivity {

private TextView txtCount, textViewTimer;
private Button btnCount;
int count = 0;
boolean[] timerProcessing = { false };
boolean[] timerStarts = { false };
private MyCount timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView digital= (TextView) findViewById(R.id.textView2);
    txtCount = (TextView) findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount  = (Button) findViewById(R.id.button1);
    Button btnRestart = (Button) findViewById(R.id.button2);
    Button btnRecord = (Button)findViewById(R.id.button_record);
    textViewTimer = (TextView) findViewById(R.id.textView2);

    timer = new MyCount(10000, 1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // start timer once when button first click
            if (!timerStarts[0]) {
                timer.start();
                timerStarts[0] = true;
                timerProcessing[0] = true;
            }

            if (timerProcessing[0]) {
                count++;
                txtCount.setText(String.valueOf(count));
            }
        }
    });
    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timerStarts[0] = false;
            timerProcessing[0] = true;
            count = 0;
            txtCount.setText(String.valueOf(count));
            timer.cancel();
            textViewTimer.setText("10:000");

            if (btnCount.isPressed()) {
                timer.start();

            }

        }

    });
    btnRestart.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vb.vibrate(1);
            return false;
        }
    });

}

public class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        textViewTimer.setText("0:000");
        timerProcessing[0] = false;

    }

    @Override
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000 + ":"
                + millisUntilFinished % 1000);

    }

}

我不知道该怎么做,请你帮帮我吗?

1 个答案:

答案 0 :(得分:0)

看起来您使用类实例变量count来保存您感兴趣的计数。 由于您的活动已被处置和重新创建,因此您需要将此变量保存在onSaveInstanceState生命周期事件处理程序中,并在onCreate中将其还原。它看起来像这样:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the current game state
    savedInstanceState.putInt("SavedCount", count);

    //Cll the superclass 
    super.onSaveInstanceState(savedInstanceState);
}

在onCreate方法中恢复变量,如下所示:

  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState); // Always call the superclass first
     // Check whether we're recreating a previously destroyed instance
     if (savedInstanceState != null) {
     // Restore value of members from saved state
        count = savedInstanceState.getInt("SavedCount");
     } else {
       count = 0;// Probably initialize members with default values for a new instance
     }
     //...rest of oncreate method
  }