1

我正在尝试显示错误消息并在 3 秒后隐藏它们。这就是我写的,但它似乎不那样工作。

            yazi.setVisibility(View.VISIBLE);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        yazi.setVisibility(View.GONE);
4

2 回答 2

7

您可以将 Handler 及其 postDelayed 方法用于您的目的:

//Show your view
yazi.setVisibility(View.VISIBLE);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        // Hide your View after 3 seconds
        yazi.setVisibility(View.GONE);
    }
}, 3000);
于 2013-10-27T11:53:14.557 回答
0

像这样更新您的代码

在你的 onCreate() 方法里面

  yazi.setVisibility(View.VISIBLE);
    Thread thread=new Thread(runnable);
     thread.start();

在你的 onCreate() 方法之外,这样做

Runnable runnable=new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub


        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          finally{


            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yazi.setVisibility(View.GONE);
                }
            });
    }
};
};

始终使用runOnUiThread进行 UI 操作。

于 2013-10-27T11:46:36.723 回答