我有一个游戏测验,如果用户的答案是错误的,则会弹出一个弹出屏幕并在屏幕上停留 3 秒钟。我用 Handler 来做这件事。由于游戏是有时间限制的,他们想出了按下后退按钮从屏幕上移除弹出窗口的解决方案:) 并继续前进。所以我禁用了该弹出活动的后退按钮。但是,现在我有另一个问题。似乎我的处理程序从最后一次点击开始计算时间,所以如果我点击弹出屏幕上的后退按钮,处理程序从那次点击开始计算时间。如果我再次点击它,它会从头开始。我尝试点击它 10-12 次,我的弹出屏幕持续了半分钟。:) 这不好。如果在他的时间内单击后退按钮,如何让我的弹出窗口持续 3 秒?
我的弹出课程:
public class WrongAnswer extends Activity{
TextView wrong;
String correctAnswer, correct;
public final int delayTime = 3000;
private Handler myHandler = new Handler();
public void onUserInteraction(){
myHandler.removeCallbacks(closePopup);
myHandler.postDelayed(closePopup, delayTime);
}
private Runnable closePopup = new Runnable(){
public void run(){
finish();
}
};
@Override
public void onBackPressed() {
//do nothing
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.wrong);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
correct = extras.getString("correctAnswer");
}
inicijalizujVarijable();
myHandler.postDelayed(closePopup, delayTime);
}
private void inicijalizujVarijable() {
wrong = (TextView) findViewById(R.id.tvWrong);
Typeface pogresanFont = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
wrong.setTypeface(pogresanFont);
Wrong.setText("Wrong answer!\nCorrect answer is:\n\n" + correct);
}
}