2015-04-04 81 views
0

我正在嘗試向我的使用webview的應用程序添加進度條或加載欄。我要申請設立運行應用程序內的所有鏈接,但我很困惑於如何實現,只要點擊一個鏈接上如何在webview中添加進度條線

一個進度條,這是我的代碼 公共類MainActivity擴展ActionBarActivity {

private WebView mWebView; 

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview); 
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    mWebView.loadUrl("http://caknowledge.in"); 
    mWebView.setWebViewClient(new MyAppWebViewClient()); 
} 

@Override 
public void onBackPressed() { 
    if(mWebView.canGoBack()) { 
     mWebView.goBack(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

private class MyAppWebViewClient extends WebViewClient { 
} 

而且XML.JAVA代碼是如下

<WebView android:id="@+id/activity_main_webview" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

回答

0

只使用進度的網頁視圖上面,當你的Web視圖啓動加載頁面使其visiible,當你的web視圖頁面加載隱藏進度

0

您可以添加以下代碼,您webviewclient顯示進度對話框

private class MyAppWebViewClient extends WebViewClient { 
    @Override 
    public void onPageStarted(WebView p_view, String p_url, Bitmap p_favIcon) 
    { 
     try { 
      if (progressDialog != null && !isFinishing()) 
       progressDialog.show(); 
     } catch (Throwable ex) { 
      //ignore 
     } 
     super.onPageStarted(p_view, p_url, p_favIcon); 
    } 
    @Override 
    public void onPageFinished(WebView p_view, String p_url) 
    { 
     try { 
      if (progressDialog != null && progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
      } 
     } catch (Throwable ex) { 
      //ignore 
     } 
     super.onPageFinished(p_view, p_url); 
    } 
} 

在活動

progressDialog = new ProgressDialog(this); 
progressDialog.setMessage("Loading.."); 

進度條聲明希望你的作品!

0

佈局代碼

<ProgressBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/progress_bar" 
    style="?android:attr/progressBarStyleHorizontal"/> 

<WebView 
    android:layout_below="@id/progress_bar" 
    android:id="@+id/webview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

Java代碼

webView.setWebViewClient(new HelloWebViewClient()); 
progressBar.setVisibility(View.INVISIBLE); 


private class HelloWebViewClient 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) 
    { 
     // TODO show you progress image 

     progressBar.setVisibility(View.VISIBLE); 

     super.onPageStarted(view, url, favicon); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) 
    { progressBar.setVisibility(View.INVISIBLE); 
     // TODO hide your progress image 
     super.onPageFinished(view, url); 
    } 

}