简体   繁体   English

立即显示随机问题

[英]Display random questions with no delay

How I would like the questions to be like;我希望问题是怎样的;

When first question is shown, users don't enter the answer.显示第一个问题时,用户不输入答案。 Instead, user has to memorize the answer and with no delay, 2nd question is shown together with the first question still being shown.相反,用户必须记住答案,并且没有延迟,第二个问题与第一个问题一起显示。 After every 2 questions, user can only enter the answer for both questions at the same time.每 2 个问题后,用户只能同时输入两个问题的答案。 However, I'm not sure how to show it in terms of codes.但是,我不确定如何在代码方面显示它。 Also, is it possible to go the next question without having to click on the enter button?另外,是否可以在不单击输入按钮的情况下转到下一个问题? if it's possible, how do I that?如果可能,我该怎么做?

I want this to happen for the next subsequent questions.我希望在接下来的后续问题中发生这种情况。 I'm still a beginner thus I need some help.我还是个初学者,所以我需要一些帮助。 I hope I am clear if not, do let me know.我希望我很清楚,如果没有,请告诉我。

These are my codes;这些是我的代码;

@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;
        }

Is it possible to show the second question after first question ?是否可以在第一个问题之后显示第二个问题? yes it can be done.是的,它可以做到。 Use the handler.使用处理程序。 When you click the button send a message and show the first question.当您单击按钮时,发送消息并显示第一个问题。 After that show a handler message with delayed time to show the second question.之后显示带有延迟时间的处理程序消息以显示第二个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM