【问题标题】:Update UI with Thread sleep使用线程睡眠更新 UI
【发布时间】:2012-06-09 23:22:59
【问题描述】:

我正忙于为 Android 设备制作应用程序。现在我正在测试一些东西。

我想改变背景颜色的次数有限,比如说5次。每次背景改变,我希望它在2-3秒后再次改变。

如果我使用的是 Thread 类,它会在 Thread 完成后加载整个模板,您看不到颜色的变化,但它们在“背景”中运行(我可以在 LogCat 中看到)。

希望有教程或者例子可以使用。

谢谢!

【问题讨论】:

    标签: android multithreading sleep


    【解决方案1】:

    在您的 UI 线程中使用处理程序:

    Handler mHandler = new Handler();
    
    Runnable codeToRun = new Runnable() {
        @Override
        public void run() {
            LinearLayout llBackground = (LinearLayout) findViewById(R.id.background);
            llBackground.setBackgroundColor(0x847839);
        }
    };
    mHandler.postDelayed(codeToRun, 3000);
    

    处理程序将在指定时间后在 UI 线程上运行您想要的任何代码。

    【讨论】:

    • 如此简单而有效。这非常适合添加延迟线程。谢谢你扎伊德!
    【解决方案2】:

    我最近学会了如何做到这一点。这里有一个很好的教程: http://www.vogella.com/articles/AndroidPerformance/article.html#handler

    一开始有点棘手,你在主线程上执行,你启动一个子线程,然后回发到主线程。

    我做了这个小活动来打开和关闭按钮以确保我知道发生了什么:

    公共类 HelloAndroidActivity 扩展 Activity {

    /** Called when the activity is first created. */
    
    
    
       Button b1;
    
       Button b2;
    
       Handler myOffMainThreadHandler;
    
       boolean showHideButtons = true;
    
    
    @Override
    
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);   
    
        setContentView(R.layout.main);
    
        myOffMainThreadHandler = new Handler();  // the handler for the main thread
    
    
        b1 = (Button) findViewById(R.id.button1);
    
        b2 = (Button) findViewById(R.id.button2);
    
    
    }
    
    
    
    public void onClickButton1(View v){ 
    
    
    
       Runnable runnableOffMain = new Runnable(){
    
    
    
                    @Override
    
                    public void run() {  // this thread is not on the main
    
                           for(int i = 0; i < 21; i++){
    
                                 goOverThereForAFew();
    
    
    
                                 myOffMainThreadHandler.post(new Runnable(){  // this is on the main thread
    
                                        public void run(){
    
                                               if(showHideButtons){
    
                                               b2.setVisibility(View.INVISIBLE);          
    
                                               b1.setVisibility(View.VISIBLE);
    
                                               showHideButtons = false;
    
                                               } else {
    
                                               b2.setVisibility(View.VISIBLE);          
    
                                               b1.setVisibility(View.VISIBLE);
    
                                               showHideButtons = true;
    
                                               }
    
                                        }
    
                                 }); 
    
    
    
                           }
    
                    }
    
    
    
       };
    
    
    
       new Thread(runnableOffMain).start();
    
    
    
    }
    
    
    
       private void goOverThereForAFew() {
    
             try {
    
                    Thread.sleep(500);
    
             } catch (InterruptedException e) {                   
    
                    e.printStackTrace();
    
             }
    
       }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多