如何在我们恢复游戏时显示文本视图,并在几秒钟后使其隐藏

时间:2015-07-27 16:39:23

标签: java android

我正在开发一款游戏。在我的游戏中我有一个关闭并恢复游戏的对话框。我想要的是当我按下对话框的“否”按钮然后开始计数3到0然后恢复游戏。请帮助我谢谢。

public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {


        GamePanel.thread.setRunning(false);
        // in the next line of code we also style the dialog through xml which i put in styles
        AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
        alertDialog.setTitle("Exit Alert");
        alertDialog.setMessage("Do you really want to exit the Game?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which) {
                //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                finish();
                System.exit(0);
                return;

            } });
        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();




                GamePanel.thread.setRunning(true);
                return;
            }});
        alertDialog.show();

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

2 个答案:

答案 0 :(得分:1)

使用CountDownTimer

在NO按钮中写下以下行,单击evant

   new CountDownTimer(3000,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                textview.settext(millisUntilFinished);
            }

            @Override
            public void onFinish() {

            }
        };

答案 1 :(得分:0)

您可以使用处理程序在给定的时间间隔后在UI线程上发布任务:

alertDialog.setButton2("No",new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int which){

        dialog.dismiss();

        new CountDownTimer(3000,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                textView.setText(""+(millisUntilFinished%1000));
            }

            @Override
            public void onFinish() {
                textView.setVisibility(View.GONE);
                GamePanel.thread.setRunning(true);
            }
        };

        reurn;
    }
});