Android 输入/停止接收输入后的延迟方法
因此,我正在尝试创建一个类似琐事的问答游戏,两个人可以在同一部手机上进行面对面的游戏。一切都是完美的,除了当你选择一个答案时,它会很快生成下一个问题,而另一个人最终会找到一个随机的答案 例如,如果两个玩家几乎同时按下按钮,则第一个和第二个之间的秒数会生成一个新问题,第二个玩家最终会单击另一个随机问题的随机答案 有人能帮我理解在选择答案后如何延迟下一个问题,以及在延迟发生时按钮停止接收输入吗 代码在下面,但实际上我只需要知道方向,不需要特别针对我的代码 谢谢Android 输入/停止接收输入后的延迟方法,android,Android,因此,我正在尝试创建一个类似琐事的问答游戏,两个人可以在同一部手机上进行面对面的游戏。一切都是完美的,除了当你选择一个答案时,它会很快生成下一个问题,而另一个人最终会找到一个随机的答案 例如,如果两个玩家几乎同时按下按钮,则第一个和第二个之间的秒数会生成一个新问题,第二个玩家最终会单击另一个随机问题的随机答案 有人能帮我理解在选择答案后如何延迟下一个问题,以及在延迟发生时按钮停止接收输入吗 代码在下面,但实际上我只需要知道方向,不需要特别针对我的代码 谢谢 public class DuelMe
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));
}
}
public class DuelMenu扩展了AppCompative活动{
按钮answer1,answer2,answer3,answer4,再次播放,返回主页,answer1p2,answer2p2,answer3p2,answer4p2;
文本视图分数、问题、问题2、分数2;
private MultiplyQuestions mQuestions=新的MultiplyQuestions();
私人字符串mAnswer;
私有int mScore=0;
私有int mScore2=0;
private int mQuestionLength=mQuestions.mQuestions.length;
随机rng=新随机();
TextView mTextField,timer2;
倒计时游戏计时器;
布尔博弈论;
文本视图;
公共无效StartName(){
startGameTimer();
}
公共无效StartGatimer(){
gameTimer=新的倒计时(300001000){
@覆盖公共void onTick(长MillsUntilFinished){
mTextField.setText(“剩余时间:+millisuntillfinished/1000”);
timer2.setText(“剩余时间:+millisuntillfinished/1000”);
}
@重写公共void onFinish(){
gameTimerIsRunning=false;
gameOver();
}
};
gameTimer.start();
}
@凌驾
public void onBackPressed(){
moveTaskToBack(真);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
覆盖转换(R.anim.fadein,R.anim.fadeout);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity\u duel\u菜单);
answer1=(按钮)findViewById(R.id.answer1);
answer2=(按钮)findViewById(R.id.answer2);
answer3=(按钮)findViewById(R.id.answer3);
answer4=(按钮)findViewById(R.id.answer4);
answer1p2=(按钮)findViewById(R.id.answer1p2);
answer2p2=(按钮)findViewById(R.id.answer2p2);
answer3p2=(按钮)findViewById(R.id.answer3p2);
answer4p2=(按钮)findViewById(R.id.answer4p2);
分数=(TextView)findViewById(R.id.score);
score2=(TextView)findViewById(R.id.score2);
问题=(TextView)findViewById(R.id.question);
问题2=(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);
分数2.setText(“分数”+mScore2);
Typeface custom_font=Typeface.createFromAsset(getAssets(),“fonts/Monster.ttf”);
score.setTypeface(自定义字体);
分数2.设置字体(自定义字体);
问题.设置字体(自定义字体);
问题2.设置字体(自定义字体);
mTextField.setTypeface(自定义字体);
timer2.setTypeface(自定义字体);
回答1P2.设置字体(自定义字体);
回答2P2.设置字体(自定义字体);
回答3P2.设置字体(自定义字体);
回答4P2.设置字体(自定义字体);
回答1.设置字体(自定义字体);
回答2.设置字体(自定义字体);
回答3.设置字体(自定义字体);
回答4.设置字体(自定义字体);
Button advanceToDuelFriend=(按钮)findViewById(R.id.playReach);
advanceToDuelFriend.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图agr0){
意向意向=新意向(DuelMenu.this,DuelMenu.class);
星触觉(意向);
}
});
按钮高级HOME=(按钮)findViewById(R.id.returnHome);
advanceToHome.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图agr0){
意向意向=新意向(DuelMenu.this,MainActivity.class);
星触觉(意向);
}
});
最终动画fadeInAnimation=AnimationUtils.loadAnimation(这是R.anim.fadeoutcorrect);
updateQuestion(rng.nextInt(mQuestionLength));
startGame();
answer1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(answer1.getText()==mAnswer){
mScore++;
score.setText(“score”+mScore);
updateQuestion(rng.nextInt(mQuestionLength));
}否则{
mScore--;
score.setText(“score”+mScore);
updateQuestion(rng.nextInt(mQuestionLength));
}
}
});
answer2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(answer2.getText()==mAnswer){
mScore++;
score.setText(“score”+mScore);
updateQuestion(rng.nextInt(mQuestionLength));
}否则{
mScore--;
score.setText(“score”+mScore);
updateQuestion(rng.nextInt(mQuestionLength));
}
}
});
answer3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(answer3.getText()==mAnswer){
mScore++;
score.setText(“score”+mScore);
updateQuestion(rng.nextInt(mQuestionLength));
}否则{
mScore--;
得分
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();
}