android在计时器上设置按钮的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3392147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:00:25  来源:igfitidea点击:

android set visibility of a button on timer

androidmultithreadingbuttonviewtimer

提问by steve

I have an app that shows a disclaimer at the beginning of the program. I want a button to remain invisible for a set amount of time, and then become visible. I set up a thread that sleeps for 5 seconds, and then tries to make the button visible. However ,I get this error when I execute my code:

我有一个在程序开头显示免责声明的应用程序。我希望一个按钮在一段时间内保持不可见,然后变为可见。我设置了一个休眠 5 秒的线程,然后尝试使按钮可见。但是,当我执行代码时出现此错误:

08-02 21:34:07.868: ERROR/AndroidRuntime(1401): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

08-02 21:34:07.868: 错误/AndroidRuntime(1401): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。

How can I count 5 seconds, and then make the button visible? THanks.

如何计算 5 秒,然后使按钮可见?谢谢。

Thread splashTread = new Thread() {
           @Override
           public void run() {
            try {
                   int waited = 0;
                   while(_active && (!_ok2)) {
                       sleep(100);
                       if(_active) {
                           waited += 100;
                           if(waited >= _splashTime)
                           {
                            turnButtonOn();
                           }

                       }
                   }
               } catch(InterruptedException e) {
                   // do nothing
               } finally {
                   finish();
                   startActivity(new Intent("com.lba.mixer.Choose"));

               }
    };
    splashTread.start();


      public static void turnButtonOn() {
         okButton.setVisibility(View.VISIBLE);
      }

回答by Andy Zhang

The problem is that you're not in the UI thread when you call okButton.setVisibility(View.VISIBLE);, since you create and run your own thread. What you have to do is get your button's handler and set the visibility through the UI thread that you get via the handler.

问题是当您调用 时,您不在 UI 线程中okButton.setVisibility(View.VISIBLE);,因为您创建并运行了自己的线程。您需要做的是获取按钮的处理程序并通过您通过处理程序获得的 UI 线程设置可见性。

So instead of

所以代替

okButton.setVisibility(View.VISIBLE)

you should do

你应该做

okButton.getHandler().post(new Runnable() {
    public void run() {
        okButton.setVisibility(View.VISIBLE);
    }
});

回答by user1730217

I found this to be a much simpler solution. Visibility on 7 second delay

我发现这是一个更简单的解决方案。7 秒延迟的可见性

continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
        public void run() {
            continuebutton.setVisibility(View.VISIBLE);
        }
    }, 7000);

回答by Avinash jain

I found this a Better solution to the problem (button id = but_resend)

我发现这是一个更好的问题解决方案(按钮 id = but_resend)

define handler

定义处理程序

  private Handler handler;

call function in extend class

扩展类中的调用函数

 showButtons();

define after class

课后定义

private void showButtons() {
        handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ((Button) findViewById(R.id.but_resend)).setVisibility(View.VISIBLE);
            }
        }, 20000); // produce 20 sec delay in button visibility


    }

and keep in mind to hide the visibility in the.xml file by

并记住通过以下方式隐藏 .xml 文件中的可见性

android:visibility="invisible"