Android问答计时器问题

Android问答计时器问题,android,countdowntimer,Android,Countdowntimer,实施1问题 疑问1:我尝试使用进度条计时。比如说每个问题5秒钟。但是,进度条确实显示倒计时值。我的问题是在时间结束后(值达到0),默认情况下,我不会转到下一个问题 实施2问题 疑问2:另外,我如何使用进度条设置倒计时计时器,该进度条从第一个问题开始,不断减小数值,直到指定的限制。比如说,我希望测验中的所有20个问题都能在120分钟内得到回答。 所以进度条应该是120119…1。在达到0后,我应该自动重定向到结果页面 QuestionActivity.java package.really.goo

实施1问题

疑问1:我尝试使用进度条计时。比如说每个问题5秒钟。但是,进度条确实显示倒计时值。我的问题是在时间结束后(值达到0),默认情况下,我不会转到下一个问题

实施2问题

疑问2:另外,我如何使用进度条设置倒计时计时器,该进度条从第一个问题开始,不断减小数值,直到指定的限制。比如说,我希望测验中的所有20个问题都能在120分钟内得到回答。 所以进度条应该是120119…1。在达到0后,我应该自动重定向到结果页面

QuestionActivity.java
package.really.good有效;
导入java.sql.Date;
导入java.util.List;
导入android.app.Activity;
导入android.content.Intent;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.text.format.DateFormat;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ProgressBar;
导入android.widget.RadioButton;
导入android.widget.TextView;
导入works.really.good.quick.GamePlay;
导入works.really.good.quick.Question;
导入works.really.good.util.Utility;
公共类QuestionActivity扩展了活动实现OnClickListener{
私人问题;
私人游戏;
ProgressBar-mProgressBar;
倒数计时器mCountDownTimer;
文本视图电视;
int i=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
currentGame=((MyApplication)getApplication()).getCurrentGame();
currentQ=currentGame.getNextQuestion();
按钮nextBtn=(按钮)findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(此);
设置问题();
tv=(文本视图)findViewById(R.id.tv\U计数器);
mProgressBar=(ProgressBar)findViewById(R.id.ProgressBar);
mProgressBar.setProgress(i);
mCountDownTimer=新的倒计时(50001000){
@凌驾
公共void onTick(长毫秒未完成){
Log.v(“日志标签”、“进度标记”+i+millisuntilfooted);
i++;
字符串时间=(String)DateFormat.format(“ss”,新日期(millisUntilFinished));
tv.setText(时间);
mProgressBar.setProgress(i);
}
@凌驾
公共无效onFinish(){
//做你想做的
i++;
mProgressBar.setProgress(i);
}
};
mCountDownTimer.start();
}
私人问题{
//从当前问题设置问题文本
字符串问题=Utility.capitalize(currentQ.getQuestion())+“?”;
TextView qText=(TextView)findViewById(R.id.question);
qText.setText(问题);
//设置可用选项
列表答案=currentQ.getQuestionOptions();
TextView选项1=(TextView)findViewById(R.id.answer1);
选项1.setText(Utility.capitalize(answers.get(0));
TextView选项2=(TextView)findViewById(R.id.answer2);
选项2.setText(Utility.capitalize(answers.get(1));
TextView选项3=(TextView)findViewById(R.id.answer3);
选项3.setText(实用程序.大写(答案.获取(2));
TextView选项4=(TextView)findViewById(R.id.answer4);
选项4.setText(实用程序.capitalize(answers.get(3));
}
@凌驾
公共void onClick(视图arg0){
/**
*验证已选中的复选框
*/
如果(!checkAnswer())返回;
/**
*检查游戏是否结束
*/
if(currentGame.isGameOver()){
意向i=新意向(此,EndgameActivity.class);
星触觉(i);
完成();
}
否则{
意图i=新意图(此,QuestionActivity.class);
星触觉(i);
完成();
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
开关(钥匙代码)
{
case KeyEvent.KEYCODE\u返回:
返回true;
}
返回super.onKeyDown(keyCode,event);
}
私有布尔校验应答(){
字符串应答=getSelectedAnswer();
如果(答案==null){
返回false;
}
否则{
if(currentQ.getAnswer().equalsIgnoreCase(answer))
{
currentGame.incrementRightAnswers();
}
否则{
currentGame.IncrementErrorAnswers();
}
返回true;
}
}
私有字符串getSelectedAnswer(){
RadioButton c1=(RadioButton)findViewById(R.id.answer1);
RadioButton c2=(RadioButton)findViewById(R.id.answer2);
RadioButton c3=(RadioButton)findViewById(R.id.answer3);
RadioButton c4=(RadioButton)findViewById(R.id.answer4);
if(c1.isChecked())
{
if(currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString())
{
c1.setTextColor(颜色为绿色);
}
其他的
{
c1.setTextColor(颜色为红色);
if(currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString())
{
c2.setTextColor(颜色为绿色);
}
else if(currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString())
{
c3.setTextColor(颜色为绿色);
package works.really.good;

import java.sql.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import works.really.good.quiz.GamePlay;
import works.really.good.quiz.Question;
import works.really.good.util.Utility;

public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;
    ProgressBar mProgressBar;
    CountDownTimer mCountDownTimer;
    TextView tv;
    int i=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        currentGame = ((MyApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn = (Button) findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(this);
        setQuestions();

        tv = (TextView) findViewById(R.id.tv_counter);
        mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
        mProgressBar.setProgress(i);
           mCountDownTimer=new CountDownTimer(5000,1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
                    i++;
                    String time = (String) DateFormat.format("ss", new Date(millisUntilFinished));
                    tv.setText(time);
                    mProgressBar.setProgress(i);

                }

                @Override
                public void onFinish() {
                //Do what you want 
                    i++;
                    mProgressBar.setProgress(i);
                }
            };
            mCountDownTimer.start();
        
    }


    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) {
            
        /**
         * validate a checkbox has been selected
         */
        if (!checkAnswer()) return;

        
        /**
         * check if end of game
         */
        if (currentGame.isGameOver()){
            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);
    }


    private boolean checkAnswer() {
        String answer = getSelectedAnswer();
        if (answer==null){
            return false;
        }
        else {
                if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                    currentGame.incrementRightAnswers();
            }
            else{
                    currentGame.incrementWrongAnswers();
            }
            return true;
        }
    }


    private String getSelectedAnswer() {
        RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
        RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
        RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
        RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
        if (c1.isChecked())
        {
            if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
            {
                c1.setTextColor(Color.GREEN);
            }
            else
            {
                c1.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }
                
            }
            return c1.getText().toString();
        }
        if (c2.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
            {
                c2.setTextColor(Color.GREEN);
            }   
            else
            {
                c2.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }
            }
            return c2.getText().toString();
        }
        if (c3.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
            {
                c3.setTextColor(Color.GREEN);
            }
            else
            {
                c3.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }
            }
            return c3.getText().toString();
        }
        if (c4.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
            {
                c4.setTextColor(Color.GREEN);
            }
            else
            {
                c4.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
            }
            return c4.getText().toString();
        }
        
        return null;
    }
    
}
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_horizontal"
    android:background="@drawable/background">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="15dip" android:paddingBottom="15dip"
        android:gravity="center_horizontal">

        <ImageView android:id="@+id/logo" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:src="@drawable/logo2"
            android:contentDescription="Quiz_Logo" />

    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip"
        android:gravity="center_horizontal" >

        <RadioGroup android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:id="@+id/group1">
            
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textColor="#ffffff"
                android:textStyle="bold" android:id="@+id/question"/>
            
            <RadioButton android:checked="false" android:id="@+id/answer1" />
            
            <RadioButton android:checked="false" android:id="@+id/answer2" />
            
            <RadioButton android:checked="false" android:id="@+id/answer3" />
            
            <RadioButton android:checked="false" android:id="@+id/answer4" />
            
        </RadioGroup>

    </LinearLayout>
    
    

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip"
        android:gravity="center_horizontal">

        <Button android:text="Next" android:id="@+id/nextBtn"
            android:layout_width="80dip" 
            android:layout_height="wrap_content" android:background="@drawable/button_selector"
            android:paddingTop="5dip" android:paddingBottom="5dip"
            android:textColor="#ffffff" />              

    </LinearLayout>     
    
    <LinearLayout android:orientation="horizontal"
        android:layout_width="290.0dip" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip" android:layout_marginTop="50dp"
        android:gravity="center_horizontal" >                   
        
    <ProgressBar 
    android:id="@+id/progressbar"  
    android:max="5" 
    android:progress="0" 
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" 
    style="@android:style/Widget.ProgressBar.Horizontal"
    />      
    
    <TextView 
        android:id="@+id/tv_counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"/>
    </LinearLayout>     

</LinearLayout>