2014-11-22 96 views
0

我想在启动Android应用程序时显示“加载...”消息。它应该显示3秒。启动Android应用程序时“加载”消息不会消失

我试图用下面的代码实现这一点,但问题是它一直显示“加载...”消息。当我点击背景时,它会消失,但这绝对不是我在使用此代码时想到的。

有人可以帮我一个这个吗?

public class MainActivity extends Activity { 

//private Button button; 
private WebView webView; 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    setContentView(R.layout.activity_main); 

    //Get webview 
    webView = (WebView) findViewById(R.id.webView1); 

    startWebView("http://example.com"); 

} 

private void startWebView(String url) { 

    //Create new webview Client to show progress dialog 
    //When opening a url or click on link 

    webView.setWebViewClient(new WebViewClient() {  


     //If you will not use this method url links are open in new brower not in webview 
     public boolean shouldOverrideUrlLoading(WebView view, String url) {    
      view.loadUrl(url); 
      return true; 
     } 

    }); 

    webView.setWebChromeClient(new WebChromeClient() { 
     //Show loader on url load 
     ProgressDialog progressDialog; 
     public void onProgressChanged(WebView view, int progress) { 
      if (progressDialog == null) { 
       progressDialog = new ProgressDialog(MainActivity.this); 
       progressDialog.setMessage("Loading..."); 
       progressDialog.show(); 
      } 

      if(progress==3000 && progressDialog.isShowing()){ 
       progressDialog.dismiss(); 
       progressDialog = null; 
      } 
     } 
    }); 

    // Javascript inabled on webview 
    webView.getSettings().setJavaScriptEnabled(true); 

    //Load url in webview 
    webView.loadUrl(url);  

} 

// Open previous opened link from history on webview when back button pressed 

@Override 
// Detect when the back button is pressed 
public void onBackPressed() { 
    if(webView.canGoBack()) { 
     webView.goBack(); 
    } else { 
     // Let the system handle the back button 
     super.onBackPressed(); 
    } 
} 

} 
+0

[“加载”消息的可能重复之后的进度对话框启动Android应用时不会消失(http://stackoverflow.com/questions/27076506/loading-message-when-starting-android-app-doesnt-disappear) – njzk2 2014-11-25 19:22:33

回答

0

也许你可以做这样的事情:

progressDialog.show(); 
new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
    progressDialog.dismiss(); 
    } 
}, 3000); 

这将解雇自动3秒

相关问题