如何分别从其他类及其 TextView 触发计时器并分别在另一个 xml 和类中实现? [英] how to trigger timer from other class and its TextView and implementation in another xml and class respectively?
问题描述
我正在做 android 测验,并希望在我的每个问答页面上都有计时器.我的测验中有菜单页面和开始游戏的播放按钮.我希望当我点击播放按钮时触发这个计时器.为此,我必须在表示我的菜单页面的 XML 问题中创建 TextView.并在代表我的第一个问题页面的 QuestionActivity 类中实现.我也在发布 WelcomeActivity 类,尽管它在这个问题中没有任何作用.
I am working on android quiz and want timer on my each question-answer page. I have menu page in my quiz and play button to start game. And i want this timer is triggered when i click on play button. For this i have to create TextView in question XML that represent my menu page. And implementation in QuestionActivity class which represent my first question page. i am also posting WelcomeActivity class although its not play any role in this question.
播放按钮布局
<Button
android:text="Play"
android:id="@+id/playBtn"
android:layout_width="80dip"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textColor="#ffffff"
android:background="@drawable/start_button" />
表示 Timer 的 TextView 的问题 XML
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/question"
android:layout_centerHorizontal="true"
android:background="@drawable/timer_bttn"
android:onClick="onClick"/>
我实现计时器代码的问题活动
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
/**
* Configure current game and get question
*/
currentGame = ((CYKApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setOnClickListener(this);
Button nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setOnClickListener(this);
Button nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setOnClickListener(this);
Button nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion());
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
int score = currentGame.getScore();
String scr = String.valueOf(score);
TextView score1=(TextView) findViewById(R.id.score);
score1.setText(scr);
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(!checkAnswer(arg0)) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()) {
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer(View v) {
Button b=(Button) v;
String answer = b.getText().toString();
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else {
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore();
}
return true;
}
public void setTimer() {
long finishTime = 5;
CountDownTimer counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
public void onFinish() {
//code to execute when time finished
}
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
txtTimer.setText("" + minutes + ":0" + seconds);
} else {
txtTimer.setText("" + minutes + ":" + seconds);
}
}
};
counterTimer.start();
}
}
欢迎活动
public class WelcomeActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
//////////////////////////////////////////////////////////////////////
//////// GAME MENU /////////////////////////////////////////////////
Button playBtn = (Button) findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);
Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
settingsBtn.setOnClickListener(this);
Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
rulesBtn.setOnClickListener(this);
Button exitBtn = (Button) findViewById(R.id.exitBtn);
exitBtn.setOnClickListener(this);
}
/**
* Listener for game menu
*/
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
//once logged in, load the main page
//Log.d("LOGIN", "User has started the game");
//Get Question set //
List<Question> questions = getQuestionSetFromDb();
//Initialise Game with retrieved question set ///
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((CYKApplication)getApplication()).setCurrentGame(c);
//Start Game Now.. //
i = new Intent(this, QuestionActivity.class);
startActivityForResult(i, Constants.PLAYBUTTON);
break;
case R.id.rulesBtn :
i = new Intent(this, RulesActivity.class);
startActivityForResult(i, Constants.RULESBUTTON);
break;
case R.id.settingsBtn :
i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, Constants.SETTINGSBUTTON);
break;
case R.id.exitBtn :
finish();
break;
}
}
推荐答案
正如 Piyush Gupta 所提到的,你应该在 QuestionActivity
中从 onResume
调用 setTimer()
方法.
As mentioned by Piyush Gupta, you should call the setTimer()
method from onResume
in your QuestionActivity
.
来自 Android 开发者文档:
(onResume is) 调用您的 Activity 开始与用户交互.这是开始动画、打开独占访问设备(如相机)等的好地方.
在您的代码中,您还应该使您在 setTimer()
中使用的 counterTimer
成为类成员,而不是局部变量;如果不这样做,它会在 setTimer()
调用完成后超出范围,并且您将无法访问它.
In your code you should also make the counterTimer
you use in setTimer()
a class member, not a local variable; if you don't it will go out of scope once the setTimer()
call completes and your access to it will be lost.
因此您需要将以下内容添加到 QuestionActivity
:
So you will need to add the following to QuestionActivity
:
public class QuestionActivity extends Activity implements OnClickListener{
// NEW: add counterTimer as a member
private CountDownTimer counterTimer;
// NEW: implement onResume
@Override
public void onResume() {
setTimer();
super.onResume();
}
// CHANGE: setTimer should be changed as follows
public void setTimer() {
long finishTime = 5;
// NOTE: use the member, instead of a local
counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
public void onFinish() {
//code to execute when time finished
}
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
txtTimer.setText("" + minutes + ":0" + seconds);
} else {
txtTimer.setText("" + minutes + ":" + seconds);
}
}
};
counterTimer.start();
}
}
以上示例使用您现在的代码,但我建议您在 onCreate
中创建计时器,使用类成员来存储它(即当前在 setTimer() 中的所有内容, 除了 counterTimer.start();
调用.然后只需在 onResume
中使用 counterTimer.start();
.也许添加一个 counterTimer.cancel()
调用 onPause
以便当活动失去焦点时计时器结束.
This above example uses your code as it is now, but I would advise you to create the timer in onCreate
using the class member to store it (ie. everything that is currently in setTimer(), except for the counterTimer.start();
call. Then just use counterTimer.start();
in onResume
. And maybe add a counterTimer.cancel()
call to onPause
so that the timer ends when the activity loses focus.
这篇关于如何分别从其他类及其 TextView 触发计时器并分别在另一个 xml 和类中实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!