2014-03-25 86 views
0
package net.sourceforge.zbar.android.CameraTest; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 


public class WebViewActivity extends Activity { 

    private WebView webView; 

    @SuppressLint("SetJavaScriptEnabled") 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview);  
     String mUrl = getIntent().getStringExtra("mUrl"); 
     webView = (WebView) findViewById(R.id.web_engine); 
     webView.setWebViewClient(new MyWebClient()); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl(mUrl); 



    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if(event.getAction() == KeyEvent.ACTION_DOWN){ 
      switch(keyCode) 
      { 
      case KeyEvent.KEYCODE_BACK: 
       if(webView.canGoBack()){ 
        webView.goBack(); 
       }else{ 
        finish(); 
       } 
       return true; 
      } 

     } 
     return super.onKeyDown(keyCode, event); 
    } 
} 

class MyWebClient extends WebViewClient { 

    @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 


    } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 

      return false; 

     } 


     } 

我試着在此代碼中放置進度對話框,但應用程序停止。我把它放在webclient類中,但是在我運行應用程序後出現了進度對話框,但應用程序停止。我已經刪除了該部分,以便您可以看到工作代碼。我該怎麼做?webview中的進度對話框,qr代碼掃描器

回答

1

您必須在佈局中添加一個ProgressBar對象,但隱藏。然後,當頁面開始加載,你顯示進度條:

ProgressBar progress = (ProgressBar) findViewById(R.id.progress); 
progress.setVisibility(View.VISIBLE); 

你甚至可以覆蓋進度變化

@Override 
public void onProgressChanged(WebView view, int percentage) { 
    ProgressBar progress = (ProgressBar) findViewById(R.id.progress); 
    progress.setProgress(percentage); 
} 

對於這最後的方法,你必須擴展WebChromeClient。