毫不拖延地显示随机问题

时间:2015-06-27 15:25:53

标签: java android

我希望问题如何;

当显示第一个问题时,用户不会输入答案。相反,用户必须记住答案并且没有延迟,第二个问题与第一个问题一起显示。在每两个问题之后,用户只能同时输入两个问题的答案。但是,我不确定如何在代码方面显示它。此外,是否可以在不必单击输入按钮的情况下转到下一个问题?如果有可能,我该怎么做?

我希望在接下来的后续问题中实现这一点。我还是初学者因此我需要一些帮助。如果没有,我希望我很清楚,请告诉我。

这些是我的代码;

@Override
public void onClick(View view) {
    if (view.getId() == R.id.enter) {
        String answerContent = answerTxt.getText().toString();
        if(firstQuestion == true)
        {
            buttonCounter = 1;
            firstQuestion = false;
            replayBtn.setEnabled(true);
            chooseQuestion();
        } else if (!answerContent.endsWith("?")) {
            int enteredAnswer = Integer.parseInt(answerContent.substring(2));
            int exScore = getScore();
            if (enteredAnswer == answer){
                buttonCounter = 1;
                replayBtn.setEnabled(true);
                scoreTxt.setText("Score: " + (exScore + 1));
                response.setImageResource(R.drawable.tick);
                response.setVisibility(View.VISIBLE);
            } else {
                setHighScore();
                buttonCounter = 1;
                replayBtn.setEnabled(true);
                if (exScore == 0) {
                    scoreTxt.setText("Score:0");
                } else {
                    scoreTxt.setText("Score: " + (exScore - 1));
                }
                response.setImageResource(R.drawable.cross);
                response.setVisibility(View.VISIBLE);
            }
            chooseQuestion();
        }
    } else if (view.getId() == R.id.imageButton1) {

        if (buttonCounter == 1) {
            replayBtn.setEnabled(false);
            Log.d("ins", "called");
        }
        buttonCounter = 0;

        final int DELAY_MS = 1000;



        MediaPlayer firstNum = MediaPlayer.create(this,
                numberAudioList[operand1]);

        firstNum.start();
        pauseTimer = true;
        firstNum.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer firstNum) {
                firstNum.stop();
                firstNum.reset();
                firstNum.release();

                pauseTimer = false;

            }
        });

        try {
            Thread.sleep(DELAY_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        MediaPlayer operatorAudio = MediaPlayer.create(this,
                operatorAudioList[operator]);

        operatorAudio.start();
        pauseTimer = true;
        operatorAudio.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer operatorAudio) {
                operatorAudio.stop();
                operatorAudio.reset();
                operatorAudio.release();

                pauseTimer = false;
            }
        });

        try {
            Thread.sleep(DELAY_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        MediaPlayer secondNum = MediaPlayer.create(this,
                numberAudioList[operand2]);

        secondNum.start();
        pauseTimer = true;
        secondNum.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer secondNum) {
                secondNum.stop();
                secondNum.reset();
                secondNum.release();

                pauseTimer = false;
            }
        });

        if (leftTimeInMillisecondsGlobal == 0) {
            if (countDownTimer != null) {
                countDownTimer.cancel();
            }
            setTimer(originalTimerTimeInMilliSeconds);
        } else {
            if (countDownTimer != null) {
                countDownTimer.cancel();

            }
            setTimer(leftTimeInMillisecondsGlobal);
        }

        startTimer();

    } else if (view.getId() == R.id.clear) {

        cancel = MediaPlayer.create(this, R.raw.cancel);
        cancel.start();
        cancel.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer cancel) {
                cancel.stop();
                cancel.reset();
                cancel.release();
            }
        });

        answerTxt.setText("= ?");
    } else {
        response.setVisibility(View.INVISIBLE);
        int enteredNum = Integer.parseInt(view.getTag().toString());
        if (answerTxt.getText().toString().endsWith("?"))
            answerTxt.setText("= " + enteredNum);
        else
            answerTxt.append("" + enteredNum);

        MediaPlayer num = MediaPlayer.create(this,
                numberAudioList[enteredNum]);
        num.start();
        num.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer num) {
                num.stop();
                num.reset();
                num.release();

            }
        });

    }
}

private void setTimer(long timeLeft) {

    totalTimeCountInMilliseconds = timeLeft;

    timeBlinkInMilliseconds = 20 * 1000;
}

private void startTimer() {
    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            long seconds = leftTimeInMilliseconds / 1000;

            if (pauseTimer == true) {
                leftTimeInMillisecondsGlobal = leftTimeInMilliseconds;
                countDownTimer.cancel();
            } else {

                leftTimeInMillisecondsGlobal = leftTimeInMilliseconds;

                if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
                    textViewShowTime.setTextAppearance(
                            getApplicationContext(), R.style.blinkText);

                    if (blink) {
                        textViewShowTime.setVisibility(View.VISIBLE);
                    } else {
                        textViewShowTime.setVisibility(View.INVISIBLE);
                    }

                    blink = !blink;
                }

                textViewShowTime.setText(String
                        .format("%02d", seconds / 60)
                        + ":"
                        + String.format("%02d", seconds % 60));
            }
        }

        @Override
        public void onFinish() {
            Log.i("check", "check if enter onFinish() method");
            setResults();
            saveScore();
            Log.i("check", "check if passes through");
        }
    }.start();
}

private int getScore() {
    String scoreStr = scoreTxt.getText().toString();
    return Integer
            .parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ") + 1));
}

private void chooseQuestion() {
    answerTxt.setText("= ?"); //first reset the answer Text View
    operator = random.nextInt(operators.length);
    operand1 = random.nextInt(numberList.length);
    operand2 = random.nextInt(numberList.length);

    if (operator == 0) {
        do {
            operand1 = getOperand1();
            operand2 = getOperand2();
            answer = getAnswer();
        } while ((answer > 20));

        answer = operand1 + operand2;
    } else if (operator == 1) {
        do {
            operand1 = getOperand1();
            operand2 = getOperand2();
            answer = getAnswer();
        } while ((operand2 > operand1) || answer > 20);

        answer = operand1 - operand2;
    } else if (operator == 2) {

        do {
            operand1 = getOperand1();
            operand2 = getOperand2();
            answer = getAnswer();
        } while (operand1 > 12 || operand2 > 12 || (answer > 20));
        answer = operand1 * operand2;
    } else if (operator == 3) {
        do {
            operand1 = getOperand1();
            operand2 = getOperand2();
            answer = getAnswer();
        } while (((((double) operand1 / (double) operand2) % 1 > 0)
                || answer > 20 || (operand1 == operand2) || (operand1 == 0) || (operand2 == 0)));
        answer = operand1 / operand2;
    }

    final int DELAY_MS = 1000;

    // moved to onClick()



    MediaPlayer firstNum = MediaPlayer.create(this,
            numberAudioList[operand1]);

    pauseTimer = true;
    firstNum.start();
    firstNum.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer firstNum) {
            firstNum.stop();
            firstNum.reset();
            firstNum.release();
            pauseTimer = false;
        }
    });

    try {
        Thread.sleep(DELAY_MS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    MediaPlayer operatorAudio = MediaPlayer.create(this,
            operatorAudioList[operator]);

    pauseTimer = true;
    operatorAudio.start();
    operatorAudio.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer operatorAudio) {
            operatorAudio.stop();
            operatorAudio.reset();
            operatorAudio.release();
            pauseTimer = false;
        }
    });

    try {
        Thread.sleep(DELAY_MS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    MediaPlayer secondNum = MediaPlayer.create(this,
            numberAudioList[operand2]);

    pauseTimer = true;
    secondNum.start();
    secondNum.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer secondNum) {
            secondNum.stop();
            secondNum.reset();
            secondNum.release();
            pauseTimer = false;
        }
    });

    int i = getScore();
    if (i < 5) {
        for (i = 0; i < 5; i++) {

            question.setText(operand1 + " " + operators[operator] + " "
                    + operand2); //display the question to the user
        }
    } else {
        question.setText("Qns" + (i + 1));
    }

    // Moved to onClick()

    if (leftTimeInMillisecondsGlobal == 0) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        setTimer(originalTimerTimeInMilliSeconds);
    } else {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        setTimer(leftTimeInMillisecondsGlobal);
    }

    startTimer();
}

private int getOperand1() {

    operand1 = 0;
    do {
        operand1 = random.nextInt(numberList.length);
    } while (operand1 < 1 || operand1 > 20);
    return operand1;
}

private int getOperand2() {

    operand2 = 0;
    do {
        operand2 = random.nextInt(numberList.length);
    } while (operand2 < 1 || operand2 > 20);
    return operand2;
}

private int getAnswer() {       
        if (operator == 0) {
        answer = operand1 + operand2;
        }

        else if (operator == 1) {
        answer = operand1 - operand2;
        }

        else if (operator == 2) {
        answer = operand1 * operand2;
        }

        else if (operator == 3) {
        answer = operand1 / operand2;
        }
           return answer;
        }

1 个答案:

答案 0 :(得分:0)

第一个问题之后是否可以显示第二个问题?是的,它可以做到。使用处理程序。单击按钮时发送消息并显示第一个问题。之后显示处理程序消息,延迟时间显示第二个问题。

mHandler.sendEmptyMessage(FIRST_QUESTION_ID);//Fist question willbe shown immediately mHandler.sendEmptyMessageDelayed(SECOND_QUESTION_ID, 2000)// second question will be shown after 1st question after 2s