显示而在后台加载布局通过对话的setContentView [英] showing dialog while loading layout by setContentView in background

查看:110
本文介绍了显示而在后台加载布局通过对话的setContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低于code其中,我想表明,在背景前,加载内容对话框,但不能这样做。请指教。

 对话框= ProgressDialog.show(这一点,,Loading请稍候......,真正的);
runOnUiThread(新的Runnable(){
  公共无效的run(){
    的setContentView(R.layout.main_layout);
    dialog.dismiss();
  }
});


解决方案

我通过阅读下面的链接得到解决和落实code如下:
http://developer.android.com/guide/appendix/faq/commontasks html的#线程

  INT GLB = 0,glbtotal = 3;
最后的处理程序mHandler =新的处理程序();//创建可运行投寄
最终的Runnable mUpdateResults =新的Runnable(){
    公共无效的run(){
        updateResultsInUi();
    }
};公共无效open_next_screen()
    {
            对话框= ProgressDialog.show(这一点,,Loading请稍候......,真正的);
            startLongRunningOperation();
     }
私人无效updateResultsInUi(){    //早在UI线程 - 根据mResults数据更新我们的UI元素
    开关(GLB)
    {
    情况下0:
         的setContentView(R.layout.main_layout);
            。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);        打破;
    情况1:
    第2部分();    打破;
    案例2:
     第三部分();    打破;
    }}保护无效startLongRunningOperation(){    //关火一个线程做一些工作,我们不应该在UI线程直接做
    线程t =新的Thread(){
        公共无效的run(){
            为(GLB = 0; GLB&下; glbtotal; GLB ++)
            {
                mHandler.post(mUpdateResults);            开关(GLB)
            {
            情况下0:
                第1部分();
                打破;            }
            }
              dialog.dismiss();
        }
    };
    t.start();
}

I am using below code where , i want to show dialog in front and loading content in background but not able to do the same . Please advise.

dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
runOnUiThread(new Runnable(){
  public void run() {
    setContentView(R.layout.main_layout);
    dialog.dismiss();
  }
});

解决方案

I got solution by reading below link and implemented code as below: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

int glb=0,glbtotal=3;
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};

public void open_next_screen()
    {
            dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
            startLongRunningOperation();
     }
private void updateResultsInUi() {

    // Back in the UI thread -- update our UI elements based on the data in mResults
    switch(glb)
    {
    case 0:
         setContentView(R.layout.main_layout);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        break;
    case 1:
    part2();

    break;
    case 2:
     part3();

    break;
    }

}

protected void startLongRunningOperation() {

    // Fire off a thread to do some work that we shouldn't do directly in the UI thread
    Thread t = new Thread() {
        public void run() {


            for(glb=0;glb<glbtotal;glb++)
            {
                mHandler.post(mUpdateResults);

            switch(glb)
            {
            case 0:
                part1();
                break;

            }
            }
              dialog.dismiss();
        }
    };
    t.start();
}

这篇关于显示而在后台加载布局通过对话的setContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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