我正在做一个IQtest应用程序。我已经插入一个计时器来计算回答问题的时间。每当测验活动开始时计时器启动。有一个暂停按钮暂停计时器并恢复计时器。计时器正常工作当我按照上述方式进行上述步骤时。但问题是如果计时器正在运行,并且如果我恢复计时器,则计时器从先前暂停的值开始。 这是QuizActivity.java
package com.example.iqtest;
public class QuizActivity extends Activity {
List<Question> quesList;
int score,qid,temp;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc,rdd,rde;
Button butNext,agecal;
private Button pause,resume;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
timerValue = (TextView) findViewById(R.id.textView2);
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
resume = (Button) findViewById(R.id.resume);
resume.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
Bundle b = getIntent().getExtras();
qid=b.getInt("start");
temp=qid;
agecal = (Button) findViewById(R.id.buttoncalage);
agecal.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent i = new Intent(QuizActivity.this,AgeCalculation.class);
startActivity(i);
}
});
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
rde=(RadioButton)findViewById(R.id.radio4);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
if(currentQ.getanswer().equals(answer.getText()))
{
score++;
}
if(qid<(temp+20)){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getquestion());
rda.setText(currentQ.getopta());
rdb.setText(currentQ.getoptb());
rdc.setText(currentQ.getoptc());
rdd.setText(currentQ.getoptd());
rde.setText(currentQ.getopte());
qid++;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":" + String.format("%02d", secs) + ":"+ String.format("%03d", milliseconds));customHandler.postDelayed(this, 0);
}
};
}
答案 0 :(得分:1)
最优雅的解决方案是隐藏不允许用户按下的按钮。在resume的onClickListener中,添加以下行:
resume.setVisibility(View.GONE); // or View.INVISIBLE depending on what you want to do
pause.setVisibility(View.VISIBLE);
在pause的onClickListener中,添加以下行:
resume.setVisibility(View.VISIBLE);
pause.setVisibility(View.GONE);
此外,在您的布局中,默认情况下应将其中一个设置为visibility="gone"
。可能,简历。
如果您想要始终保持两个按钮都可见,那么您需要使用布尔值跟踪当前状态:resumeLastClicked
。然后,在每个onClickListener
中,检查最后button
点击了哪一个,如果您不想执行点击,则返回。