2012-01-25 70 views
2

簡單地說,我每次在我的webview中點擊一個新鏈接時,如何使進度對話框顯示出來。我已經嘗試過很多教程和方法,但是每次應用程序最初加載時,它們中的每一個都只顯示對話框一次。Webview中的進度對話框

這裏是我的代碼

mWebView.setWebViewClient(new WebViewClient() { 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       view.loadUrl(url); 
       return true; 
      } 
      ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", 
          "Loading. Please wait...", true); 
      @Override 
      public void onPageFinished(WebView view, String url) { 
      dialog.dismiss(); 
      } 
+0

工作,這將是有益的,如果你可以指定你嘗試過什麼已經。 –

+0

很多關於此StackOverflow的討論:這裏http://stackoverflow.com/questions/2537454/android-webview-progress-bar和這裏http://stackoverflow.com/questions/2496119/in-an-android -app-i-will-like-show-a--bar-on-a-child-tab-until-the-w例如 – nwaltham

+0

我知道,人們在任何地方都遇到問題。我的問題是我不想在標題中使用progressBar。如果我將它附加到onClickListener到一個按鈕,我可以輕鬆創建一個對話框。我只是不知道如何讓應用程序實現當頁面的URL改變,並顯示在該點的對話框 – Zach

回答

3

試試這個,

// Let's display the progress in the activity title bar, like the 
// browser app does. 
getWindow().requestFeature(Window.FEATURE_PROGRESS); 

webview.getSettings().setJavaScriptEnabled(true); 

final Activity activity = this; 
webview.setWebChromeClient(new WebChromeClient() { 
    public void onProgressChanged(WebView view, int progress) { 
    // Activities and WebViews measure progress with different scales. 
    // The progress meter will automatically disappear when we reach 100% 
    activity.setProgress(progress * 1000); 
    } 
}); 
webview.setWebViewClient(new WebViewClient() { 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
    } 
}); 

webview.loadUrl("http://slashdot.org/"); 

如果你想使自己的progressDialog然後

webView.setWebViewClient(new WebViewClient() { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      // Start PROGRESS DIALOG 
      view.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

     //HIDE PROGRESS DIALOG LOADING IT HAS FINISHED 
     } 

    }); 

讓我知道發生什麼事..

+0

我有問題在該點啓動進度對話框。我添加了源代碼..這是我可以讓它運行的唯一方式,但它只在應用程序第一次加載時運行 – Zach

+0

@ user1166556 - 您在做錯了在您的活動的onCreate()中創建ProgressDialog對象並僅顯示它在shouldOverrid中...並關閉pageFinished() – user370305

5

退房這樣的:

wvCouponsAndOffers.setWebViewClient(new WebViewClient() { 
     ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this); 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG); 
      progressDialog.setCancelable(false); 
      progressDialog.setOnKeyListener(new OnKeyListener(){ 
       @Override 
       public boolean onKey(DialogInterface dialog, int keyCode, 
         KeyEvent event) { 
        if(keyCode == KeyEvent.KEYCODE_SEARCH) { 
         return true; 
        } 
        else 
         return false; 
       }}); 
      progressDialog.show(); 
     } 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG); 
      progressDialog.setCancelable(false); 
      progressDialog.setOnKeyListener(new OnKeyListener(){ 
       @Override 
       public boolean onKey(DialogInterface dialog, int keyCode, 
         KeyEvent event) { 
        if(keyCode == KeyEvent.KEYCODE_SEARCH) { 
         return true; 
        } 
        else 
         return false; 
       }}); 
      progressDialog.show(); 
      view.loadUrl(url); 
      return true; 
     } 

     public void onPageFinished(WebView view, String url) { 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
      } 
     } 

    }); 
0

試試這個最簡單的一個,對我來說

public class HomeWebViewActivity extends AppCompatActivity{ 

    private String weburl; 
    private WebView webview; 
    private Toolbar toolbar; 
    private ProgressDialog prDialog; 



    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home_webview_activity); 


     toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 


     weburl="ADD YOUR URL HERE"; 

     webview = (WebView) findViewById(R.id.webviewinfo); 
     webview.setWebViewClient(new WebVwClientcls()); 


     webview.getSettings().setJavaScriptEnabled(true); 
     webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
     webview.loadUrl(weburl); 


    } 

    private class WebVwClientcls extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      prDialog = new ProgressDialog(HomeWebViewActivity.this); 
      prDialog.setMessage("Please wait ..."); 
      prDialog.show(); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      if(prDialog!=null){ 
       prDialog.dismiss(); 
      } 
     } 
    } 
}