2011-10-02 51 views
1

我有一个按钮,我不想被点击,直到一定的时间量运行(比如5秒?)我想创建这样在一段时间后可以看到Android按钮吗?

continueButtonThread = new Thread() 
    { 
     @Override 
     public void run() 
     { 
      try { 
       synchronized(this){ 
        wait(5000); 
       } 
      } 
      catch(InterruptedException ex){      
      } 

      continueButton.setVisibility(0);     
     } 
    }; 

    continueButtonThread.start(); 

线程,但我不能修改不同线程中按钮的setVisibility属性。这是来自LogCat的错误:

10-02 14:35:05.908:ERROR/AndroidRuntime(14400):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程可以触及其视图。

任何其他方式来解决这个问题?

回答

6
正确的时间

的问题是,你只能在UI触摸意见你的活动线。您可以使用runOnUiThread函数来完成。我想建议你使用

handler.postDelayed(runnable, 5000)` 
3

您必须更新从UI线程你的看法。你在做什么,你是从非ui线程更新。

使用

contextrunOnUiThread(new Runnable(){ 

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

     }}); 

或使用处理器和发送信号指示hand.sendMessage(msg)当你认为是更新视图知名度

Handler hand = new Handler()   
     { 

      @Override 
      public void handleMessage(Message msg) { 
       /// here change the visibility 
       super.handleMessage(msg); 
      } 

     }; 
0

这里是一个简单的答案,我发现

Button button = (Button)findViewBYId(R.id.button); 
button .setVisibility(View.INVISIBLE); 
button .postDelayed(new Runnable() { 
    public void run() { 
     button .setVisibility(View.VISIBLE); 
    } 
}, 7000); 
相关问题