android在计时器上设置按钮的可见性 [英] android set visibility of a button on timer

查看:24
本文介绍了android在计时器上设置按钮的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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.

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

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

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);
      }

推荐答案

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

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.

所以代替

okButton.setVisibility(View.VISIBLE)

你应该这样做

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

这篇关于android在计时器上设置按钮的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆