Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用线程睡眠更新UI_Android_Multithreading_Sleep - Fatal编程技术网

Android 使用线程睡眠更新UI

Android 使用线程睡眠更新UI,android,multithreading,sleep,Android,Multithreading,Sleep,我正忙着为android设备制作应用程序。现在我正在测试一些东西 我想改变背景颜色有限的时间,让我们说5。每次背景改变时,我希望它在2-3秒后再次改变 如果我使用Thread类,它会在线程完成后加载整个模板,你看不到颜色的变化,但它们是在“背景”中运行的(我可以在LogCat中看到) 我希望有一个教程或一个例子,我可以使用 谢谢 在UI线程中使用处理程序: Handler mHandler = new Handler(); Runnable codeToRun = new Runnable()

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

我想改变背景颜色有限的时间,让我们说5。每次背景改变时,我希望它在2-3秒后再次改变

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

我希望有一个教程或一个例子,我可以使用


谢谢

在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线程上运行您想要的任何代码。

我最近学习了如何执行此操作。这里有一个很好的教程:

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

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

公共类HelloAndroidActivity扩展了活动{

/** 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();

         }

   }
/**在首次创建活动时调用*/
按钮b1;
按钮b2;
处理程序myOffMainThreadHandler;
布尔showHideButtons=true;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myOffMainThreadHandler=新处理程序();//主线程的处理程序
b1=(按钮)findViewById(R.id.button1);
b2=(按钮)findViewById(R.id.button2);
}
公共无效按钮1(视图v){
Runnable runnableOffMain=new Runnable(){
@凌驾
public void run(){//此线程不在主线程上
对于(int i=0;i<21;i++){
gooverthereforanox();
myOffMainThreadHandler.post(新Runnable(){//这在主线程上
公开募捐{
如果(显示隐藏按钮){
b2.设置可见性(视图不可见);
b1.设置可见性(View.VISIBLE);
showHideButtons=false;
}否则{
b2.设置可见性(View.VISIBLE);
b1.设置可见性(View.VISIBLE);
showHideButtons=true;
}
}
}); 
}
}
};
新线程(runnableoffain).start();
}
私有无效gooverthereforanex(){
试一试{
睡眠(500);
}捕获(中断异常e){
e、 printStackTrace();
}
}

}

简单而有效。这非常适合添加延迟线程。谢谢你,扎伊德!