基本上,用户安装应用程序,第一次进入,并单击按钮[例如] 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);
}
}
我不知道如何做到这一点,你可以帮我吗?
我对你的问题感到困惑。你在谈论哪部分代码?你想要保存什么,你想怎么做。似乎你可以在一些类级变量中存储点击次数,除非你希望将这个计数保存在状态变化上,比如方向变化或暂停/恢复,在这种情况下,你需要将变量保存在一个包中,或者一些其他的持久性机制,如sharedPreferences。 – user1023110
** txtCount **显示您使用按钮执行的点击次数,每次用户分数高于以前的时间时,我想保存此编号。 – Pier