所以我在下面的代码中遇到两个问题,我正在寻找指导,当旋转设备时,应用程序适用于一个问题然后结束并将用户带到结果页面,我可以& #39;为了我的生命,看看为什么如果答案是正确的话会结束?
其次我在旋转设备时无法保存倒数计时器,它重置为59秒(我删除了使用的代码,因为它显然没有工作)但是我使用的是putInt( "时间",时间);
有什么想法? TIA
import java.util.List;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion, times, scored;
Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuizHelper db = new QuizHelper(this); // question class
quesList = db.getAllQuestions(); // returns all questions
currentQ = quesList.get(qid); // current question by id
txtQuestion = (TextView) findViewById(R.id.txtQuestion); //displays the current question
button1 = (Button) findViewById(R.id.button1); //button 1 displays option 1 for answer
button2 = (Button) findViewById(R.id.button2); //button 2 displays option 2 for answer
button3 = (Button) findViewById(R.id.button3); //button 3 displays option 3 for answer
scored = (TextView) findViewById(R.id.score); //Displays the score
times = (TextView) findViewById(R.id.timers); //displays the timer
setQuestionView();
CounterClass timer = new CounterClass(60000, 1000); //countdown timer set to 1 minute
CountDownTimer start = timer.start();//starts the timer
//listeners
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button1.getText().toString()); //checks if answer is correct
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button3.getText().toString());
}
});
}
public void getAnswer(String AnswerString) {
if (currentQ.getANSWER().equals(AnswerString)) {
score++; //increments the score by 1 if correct
scored.setText("Score : " + score); //updates the score display
}
else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class); //if incorrect ends the game
Bundle b = new Bundle(); //passes the score
b.putInt("score", score); // The end score
intent.putExtras(b); // displays the score on the closing screen
startActivity(intent);
}
if (qid != 21) { //if question id not equal to 21 get next question and update current question
currentQ = quesList.get(qid);
setQuestionView();
}
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
times.setText("Time is up");
onFinish();
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String timerlayout = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60); //sets the layout of the timer
System.out.println(timerlayout);
times.setText(timerlayout);
}
}
private void setQuestionView() { //creates the overall question and options for answer
txtQuestion.setText(currentQ.getQUESTION());
button1.setText(currentQ.getOPTA());
button2.setText(currentQ.getOPTB());
button3.setText(currentQ.getOPTC());
qid++; //increments the question id by 1
}
@Override
protected void onSaveInstanceState(Bundle outSate) {
outSate.putString("myQu", txtQuestion.getText().toString());
outSate.putString("1", button1.getText().toString());
outSate.putString("2", button2.getText().toString());
outSate.putString("3", button3.getText().toString());
outSate.putString("score", scored.getText().toString());
super.onSaveInstanceState(outSate);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceSate) {
super.onRestoreInstanceState(savedInstanceSate);
txtQuestion.setText( savedInstanceSate.getString("myQu"));
button1.setText( savedInstanceSate.getString("1"));
button2.setText( savedInstanceSate.getString("2"));
button3.setText( savedInstanceSate.getString("3"));
scored.setText( savedInstanceSate.getString("score"));
}
}
答案 0 :(得分:0)
排序,问题是由于计时器。问题上存在的问题仍然存在,但我对此有所了解,并会在需要时再次询问。