Android的:使用的setContentView()线程完成后, [英] Android: Use setContentView() after Thread finished

查看:275
本文介绍了Android的:使用的setContentView()线程完成后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的setContentView()方法的线程已经完成他的任务后。
但是,我怎么能认识到这一点,因为它不可能用内螺纹这种方法吗?
当在onCreate()方法使用这种方法的线程运行时,我也鸵鸟政策得到正确的结果,导致布局至极应与第一次的setContentView(R.layout.load_screen)不显示被显示​​。

我的onCreate()方法:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    //打开载入画面
    的setContentView(R.layout.load_screen);
    this.loadingScreen(); //在该方法的新的线程将启动。
}

我loadingScreen()方法:(线程完成后,我想用的setContentView()再次)

 公共无效loadingScreen(){
    // prepare一个进度条对话框
    进度=新的ProgressBar(本);
    进度=(进度)findViewById(R.id.progressBar1);    //重置进度条状态
    progressBarStatus = 0;    新主题(新的Runnable接口(){
          公共无效的run(){
        而(progressBarStatus< 100){
            尝试{
                视频下载(100);
            }赶上(InterruptedException的E1){
                // TODO自动生成catch块
                e1.printStackTrace();
            }
          //过程中的一些任务
          progressBarStatus = doSomeTasks();          //你的电脑实在是太快了,睡眠1秒
          尝试{
            视频下载(100);
          }赶上(InterruptedException的E){
            e.printStackTrace();
          }          //更新进度条
          progressBarHandler.post(新的Runnable(){
            公共无效的run(){
              progressBar.setProgress(progressBarStatus);
            }
          });
        }        // OK,文件下载,
        如果(progressBarStatus> = 100){            //睡眠2秒,这样您就可以看到100%
            尝试{
                视频下载(2000);
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }        }
      }
       })。开始();
 }


解决方案

您可以使用处理程序来更新线程内的用户界面。或U可以简单地使用runOnUiThread更新UI。和U不想使用的setContentView方法来改变用户界面,U可以简单地使用吹气布局来获取另一个布局UR的ViewGroup。

I want to use the setContentView() method after a thread has completed his task. But how could I realize this, as its not possible to use this method inside the thread? When using this method in the onCreate() method while the thread is running, I also don´t get the correct result, cause the layout wich should be displayed with the first "setContentView(R.layout.load_screen)" is not displayed.

My onCreate() Method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Open loading screen
    setContentView(R.layout.load_screen);
    this.loadingScreen();  // In this method the new Thread will start.
}

My loadingScreen() Method: (After the thread completed I would like to use setContentView() again)

 public void loadingScreen(){
    // prepare for a progress bar dialog
    progressBar = new ProgressBar(this);
    progressBar = (ProgressBar)findViewById(R.id.progressBar1);

    //reset progress bar status
    progressBarStatus = 0;

    new Thread(new Runnable() {
          public void run() {
        while (progressBarStatus < 100) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }   
          // process some tasks
          progressBarStatus = doSomeTasks();

          // your computer is too fast, sleep 1 second
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

          // Update the progress bar
          progressBarHandler.post(new Runnable() {
            public void run() {
              progressBar.setProgress(progressBarStatus);
            }
          });
        }

        // ok, file is downloaded,
        if (progressBarStatus >= 100) {

            // sleep 2 seconds, so that you can see the 100%
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
      }
       }).start();
 }

解决方案

you can use Handler to update the ui inside the thread. or u can simply use runOnUiThread to update the ui. and u dont want to use setcontentview method to change the ui, u can simply use layout inflater to fetch another layout to ur viewgroup.

这篇关于Android的:使用的setContentView()线程完成后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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