输入/停止接收输入后的延迟方法

时间:2017-05-30 20:42:52

标签: android

所以我试图创建一个像测验游戏这样的琐事,两个人可以在同一部手机上进行正面交锋。一切都很完美,除非你选择一个答案,它会如此快速地产生下一个问题,另一个人最终会得到一个随机的答案。

例如,如果两个玩家几乎同时点击按钮,则第一个和第二个之间的微小秒会产生一个新问题,第二个玩家最终会点击随机答案以获得不同的随机问题。 / p>

任何人都可以帮助我了解如何在选择答案后延迟下一个问题,以及按钮在发生此延迟时停止接收输入吗?

代码如下,但实际上我只需知道要处理的方向,不需要专门针对我的代码。

谢谢,

public class DuelMenu extends AppCompatActivity {

Button answer1, answer2, answer3, answer4, playAgain, returnHome, answer1p2, answer2p2, answer3p2, answer4p2;
TextView score, question, question2, score2;
private MultiplyQuestions mQuestions = new MultiplyQuestions();
private String mAnswer;
private int mScore = 0;
private int mScore2 = 0;
private int mQuestionLength = mQuestions.mQuestions.length;
Random rng = new Random();
TextView mTextField, timer2;
CountDownTimer gameTimer;
boolean gameTimerIsRunning;
TextView correctFade;



public void startGame(){
    startGameTimer();

}


public void startGameTimer(){
    gameTimer = new CountDownTimer(30000, 1000) {


        @Override public void onTick(long millisUntilFinished) {
            mTextField.setText("Time Remaining: " + millisUntilFinished / 1000);
            timer2.setText("Time Remaining: " + millisUntilFinished / 1000);
        }

        @Override public void onFinish() {
            gameTimerIsRunning = false;
            gameOver();

        }

    };
    gameTimer.start();
}



@Override
public void onBackPressed() {
    moveTaskToBack(true);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_duel_menu);

    answer1 = (Button) findViewById(R.id.answer1);
    answer2 = (Button) findViewById(R.id.answer2);
    answer3 = (Button) findViewById(R.id.answer3);
    answer4 = (Button) findViewById(R.id.answer4);

    answer1p2 = (Button) findViewById(R.id.answer1p2);
    answer2p2 = (Button) findViewById(R.id.answer2p2);
    answer3p2 = (Button) findViewById(R.id.answer3p2);
    answer4p2 = (Button) findViewById(R.id.answer4p2);

    score = (TextView) findViewById(R.id.score);
    score2 = (TextView) findViewById(R.id.score2);
    question = (TextView) findViewById(R.id.question);
    question2 = (TextView) findViewById(R.id.question2);

    timer2 = (TextView) findViewById(R.id.timer2);
    mTextField = (TextView) findViewById(R.id.timer);
    correctFade = (TextView) findViewById(R.id.correctFade);

    score.setText("Score "  + mScore);
    score2.setText("Score " + mScore2);


    Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/Monster.ttf");
    score.setTypeface(custom_font);
    score2.setTypeface(custom_font);
    question.setTypeface(custom_font);
    question2.setTypeface(custom_font);
    mTextField.setTypeface(custom_font);
    timer2.setTypeface(custom_font);
    answer1p2.setTypeface(custom_font);
    answer2p2.setTypeface(custom_font);
    answer3p2.setTypeface(custom_font);
    answer4p2.setTypeface(custom_font);
    answer1.setTypeface(custom_font);
    answer2.setTypeface(custom_font);
    answer3.setTypeface(custom_font);
    answer4.setTypeface(custom_font);

    Button advanceToDuelFriend = (Button) findViewById(R.id.playAgain);
    advanceToDuelFriend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View agr0) {
            Intent intent = new Intent(DuelMenu.this, DuelMenu.class);
            startActivity(intent);
        }
    });

    Button advanceToHome = (Button) findViewById(R.id.returnHome);
    advanceToHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View agr0) {
            Intent intent = new Intent(DuelMenu.this, MainActivity.class);
            startActivity(intent);
        }
    });

    final Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadeoutcorrect);


    updateQuestion(rng.nextInt(mQuestionLength));
    startGame();


    answer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer1.getText() == mAnswer) {
                mScore++;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore--;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }

    });

    answer2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer2.getText() == mAnswer) {
                mScore++;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore--;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });

    answer3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer3.getText() == mAnswer) {
                mScore++;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore--;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });

    answer4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer4.getText() == mAnswer) {
                mScore++;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));

            } else {
                mScore--;
                score.setText("Score " + mScore);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });




    answer1p2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer1p2.getText() == mAnswer) {
                mScore2++;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore2--;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }

    });

    answer2p2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer2p2.getText() == mAnswer) {
                mScore2++;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore2--;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });

    answer3p2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer3p2.getText() == mAnswer) {
                mScore2++;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            } else {
                mScore2--;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });

    answer4p2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer4p2.getText() == mAnswer) {
                mScore2++;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));

            } else {
                mScore2--;
                score2.setText("Score " + mScore2);
                updateQuestion(rng.nextInt(mQuestionLength));
            }

        }
    });






}

private void updateQuestion(int num) {



    Animation fadeIn = AnimationUtils.loadAnimation(DuelMenu.this, R.anim.enter);
    question.startAnimation(fadeIn);

    fadeIn.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

    });

    question.setText(mQuestions.getQuestion(num));
    answer1.setText(mQuestions.getChoice1(num));
    answer2.setText(mQuestions.getChoice2(num));
    answer3.setText(mQuestions.getChoice3(num));
    answer4.setText(mQuestions.getChoice4(num));

    question2.setText(mQuestions.getQuestion(num));
    answer1p2.setText(mQuestions.getChoice1(num));
    answer2p2.setText(mQuestions.getChoice2(num));
    answer3p2.setText(mQuestions.getChoice3(num));
    answer4p2.setText(mQuestions.getChoice4(num));


    mAnswer = mQuestions.getCorrectAnswer(num);


}



protected void gameOver() {
    gameTimer.cancel();

    if (mScore > mScore2) {
        Toast.makeText(this, "Player 1 Wins ", Toast.LENGTH_SHORT).show();
    }

    if (mScore < mScore2) {
        Toast.makeText(this, "Player 2 Wins", Toast.LENGTH_SHORT).show();
    }

    if (mScore == mScore2) {
        Toast.makeText(this, "IT'S A TIE!!", Toast.LENGTH_SHORT).show();
    }

    playAgain = (Button) findViewById(R.id.playAgain);
    playAgain.setVisibility(View.VISIBLE);

    returnHome = (Button) findViewById(R.id.returnHome);
    returnHome.setVisibility(View.VISIBLE);

}

public void quitToMenu(View view) {
    gameTimer.cancel();
    startActivity(new Intent(getApplicationContext(), MainActivity.class));


}



}

1 个答案:

答案 0 :(得分:0)

所以我想出了答案,我刚刚用计时器构建了一个方法,并将显示问题的方法放在计时器中。我也让按钮变得不可见,同时勾选并在完成时返回可见。

如果有更好的方法可以告诉我,那么这就足够了!

我在下面使用的方法。

public void startGameTimer2(){
    answer1.setVisibility(View.INVISIBLE);
    answer2.setVisibility(View.INVISIBLE);
    answer3.setVisibility(View.INVISIBLE);
    answer4.setVisibility(View.INVISIBLE);

    answer1p2.setVisibility(View.INVISIBLE);
    answer2p2.setVisibility(View.INVISIBLE);
    answer3p2.setVisibility(View.INVISIBLE);
    answer4p2.setVisibility(View.INVISIBLE);
    gameTimer = new CountDownTimer(1000, 1000) {


        @Override public void onTick(long millisUntilFinished) {


        }

        @Override public void onFinish() {
            gameTimerIsRunning = false;
            updateQuestion(rng.nextInt(mQuestionLength));
            answer1.setVisibility(View.VISIBLE);
            answer2.setVisibility(View.VISIBLE);
            answer3.setVisibility(View.VISIBLE);
            answer4.setVisibility(View.VISIBLE);

            answer1p2.setVisibility(View.VISIBLE);
            answer2p2.setVisibility(View.VISIBLE);
            answer3p2.setVisibility(View.VISIBLE);
            answer4p2.setVisibility(View.VISIBLE);
        }

    };
    gameTimer.start();
}