在后台通过setContentView加载布局时显示对话框

Man*_*esh 1 android android-emulator

我正在使用下面的代码,我想在前面显示对话框并在后台加载内容但不能做同样的事情.请指教.

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

Man*_*esh 6

我通过阅读以下链接获得了解决方案并实现了以下代码: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();
}
Run Code Online (Sandbox Code Playgroud)