2016-12-02 37 views
0

請我在android開發中很新,我有我的網站,我在webview中加載,現在工作我想添加一個進度條來顯示鏈接正在處理渲染和加載頁面後隱藏進度條。 請讓我看到很多的例子,但我一直在嘗試實現它時得到錯誤,請有人可以幫助我使用我自己的webview爲例。添加一個進度條到android webview java

package com.mypage.mypage; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 


public class SourceProjectActivity extends AppCompatActivity { 

    private WebView projectLoaderView; 
    private ProgressBar progress; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_source_project); 
     projectLoaderView = (WebView)findViewById(R.id.sourcejailview); 
     WebSettings webSettings = projectLoaderView.getSettings(); 

     progress = (ProgressBar) findViewById(R.id.progressBar); 
     progress.setMax(100); 

     webSettings.setJavaScriptEnabled(true); 
     webSettings.setSaveFormData(true); 
     webSettings.setDatabaseEnabled(true); 
     webSettings.setDomStorageEnabled(true); 
     //local offline loader 
     webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); 

     projectLoaderView.loadUrl("http://google.com"); 
     projectLoaderView.setWebViewClient(new WebViewClient()); 
     SourceProjectActivity.this.progress.setProgress(0); 
    } 

    private class MyWebViewClient extends WebViewClient { 
     @Override 
     public void onProgressChanged(WebView view, int newProgress) { 
      //super.onProgressChanged(view, newProgress); 
     } 
    } 

    public void setValue(int progress) { 
     this.progress.setProgress(progress); 
    } 

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

} 

這裏是我的xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_source_project" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.mypage.mypage.SourceProjectActivity"> 

    <WebView 
     android:id="@+id/sourcejailview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" > 

     <ProgressBar 
      style="@android:style/Widget.Material.Light.ProgressBar.Small" 
      android:layout_width="match_parent" 
      android:layout_x="124dp" 
      android:layout_y="43dp" 
      android:id="@+id/progressBar" 
      android:layout_height="wrap_content" /> 
    </WebView> 
</RelativeLayout> 
+0

什麼樣的錯誤? post stacktrace – snachmsm

+0

@snachmsm當我安裝apk時,它顯示的應用程序已停止工作,而我的模擬器不工作,所以我必須使用我的手機進行測試 – Alex

+0

這裏是[答](http://stackoverflow.com/questions/ 31634963/how-to-display-a-progress-bar-when-web-load-a-url-in-android)引用您正在綁定的內容以實現 – ctu

回答

1

WebViewClient沒有onProgressChanged()方法。而不是你可以使用WebChromeClient並設置進度條如下:

private class MyWebViewClient extends WebViewClient { 
     public void onProgressChanged(WebView view, int newProgress) { 
       progress.setProgress(newProgress); 
       //Maybe you want to hide it once it reaches 100 
       if (newProgress == 100){ 
         progress.setVisibility(ProgressBar.GONE); 
       } 
     } 
} 
+0

無法解析符號'progressBar' – Alex

+0

@Alex請標記爲答案,如果它適合你。 – Rahil2952