进度条基于正确的答案

时间:2016-11-23 12:17:27

标签: java android progress-bar

我创建了一个简单的数学游戏,我想根据正确的问题答案更新我的进度条。 示例:每次我正确回答问题时,进度条应该达到10%等等。

public class QuestionActivity extends Activity 
    {
    List<Question> quesList;
    int score = 0;
    int qid = 0;

    Question currentQ;
    TextView txtQuestion, times, scored;
    Button button1, button2, button3;
    ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    QuizHelper db = new QuizHelper(this);  // my question bank class
    quesList = db.getAllQuestions();  // this will fetch all quetonall               

    questions
    currentQ = quesList.get(qid); // the current question

    // the textview in which the question will be displayed
    txtQuestion = (TextView) findViewById(R.id.txtQuestion);

    //the progress bar in which progress will be displayed
    pb = (ProgressBar)findViewById(R.id.progressBar);

    // the three buttons,
    // the idea is to set the text of three buttons with the options from  

    question bank
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);

    // the textview in which score will be displayed
    scored = (TextView) findViewById(R.id.score);

    // the timer
    times = (TextView) findViewById(R.id.timers);

    // method which will set the things up for our game
    setQuestionView();
    times.setText("00:02:00");

    // A timer of 60 seconds to play for, with an interval of 1 second (1000   

    milliseconds)
    CounterClass timer = new CounterClass(60000, 1000);
    timer.start();

    // button click listeners
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // passing the button text to other method
            // to check whether the answer is correct or not
            // same for all three buttons
            getAnswer(button1.getText().toString());
            setProgressBar();
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button2.getText().toString());
            setProgressBar();
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button3.getText().toString());
            setProgressBar();
        }
    });
}

public void getAnswer(String AnswerString) {
    if (currentQ.getAnswer().equals(AnswerString)) {

        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
        score++;
        scored.setText("Score : " + score);
    } else {
        // if unlucky start activity and finish the game

        Intent intent = new Intent(QuestionActivity.this,
                ResultActivity.class);

        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }
    if (qid < 20) {
        // if questions are not over then do this
        currentQ = quesList.get(qid);
        setQuestionView();
    } else {
        // if over do this
        Intent intent = new Intent(QuestionActivity.this,
                ResultActivity.class);
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        times.setText("Time is up");
    }

    //@Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

        long millis = millisUntilFinished;
        String hms = String.format(
                "%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                        .toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                        .toMinutes(millis)));
        System.out.println(hms);
        times.setText(hms);
    }
}

private void setQuestionView() {
    // the method which will put all things together
    txtQuestion.setText(currentQ.getQuestion());
    button1.setText(currentQ.getOptionA());
    button2.setText(currentQ.getOptionB());
    button3.setText(currentQ.getOptionC());

    qid++;
}

private void setProgressBar()
{

}

}

2 个答案:

答案 0 :(得分:0)

private void setProgressBar()
 {
   int progress=pb.getProgress();

 if(progress<100)
    pb.setProgress(progress+10)


 }

这应该可以解决问题,每次调用setProgressBar()方法

时,它都会增加10点

答案 1 :(得分:0)

第一: 设置进度条限制

pb.setMax(Total Quest * 10)

稍后

每个Correct Answer Inc得分++;

private void setProgressBar()
 {
    pb.setProgress(score*10);
 }