我正在研究Timer。我在我的问题XML(我需要实现计时器以显示)中创建TextView
并在欢迎XML中创建PlayButton
(单击“播放”按钮时启动问题活动中的计时器)。我有WelcomeActivity
我已经实现了PlayButton
和QuestionActivity
我想在哪里使用我的计时器。我在这里附上我的代码: -
欢迎使用XML: -
<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" />
问题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 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;
}
}
QuestionActivity: -
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
protected TextView txtTimer;
@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)));
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
setTimer();
/**
* 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() {
String answer = getSelectedAnswer();
if (answer==null){
//Log.d("Questions", "No Checkbox selection made - returning");
return false;
}
else {
//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;
}
}
/**
*
*/
private String getSelectedAnswer() {
Button c1 = (Button)findViewById(R.id.answer1);
Button c2 = (Button)findViewById(R.id.answer2);
Button c3 = (Button)findViewById(R.id.answer3);
Button c4 = (Button)findViewById(R.id.answer4);
if (c1.callOnClick())
{
return c1.getContext().toString();
}
if (c2.callOnClick())
{
return c2.getContext().toString();
}
if (c3.callOnClick())
{
return c3.getContext().toString();
}
if (c4.callOnClick())
{
return c4.getContext().toString();
}
return null;
}
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();
}
}
我正在尝试在QuestionActivity中添加一个函数但我不知道如何继续调用我的计时器一个活动到另一个Activity。因此,在欢迎XMl中单击播放按钮后,我的计时器显示在问题XMl上。任何人都可以帮助我如何在另一个活动中添加监听器或其他一些方法来解决我的问题。
提前感谢
答案 0 :(得分:0)
为什么单击按钮时会启动计时器?
您可以将按钮设置为转到下一个活动,并在
时启动该活动的计时器它被称为......
(对不起,这就是我对您的问题的理解:您希望定时器在单击按钮并且计时器处于其他活动时启动?)