android之WebView 细节

package com.example.huadong;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ViewFlipper;

/**
 * ViewFlipperTest.java
 * @author Cloay
 * 2011-6-24
 */
public class MainActivity extends Activity  {
 
private WebView webView=null;
private AlertDialog alertDialog;//添加进度条
private ProgressDialog progressBar;

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

  
  initWebView();
 

 
    
  
 }
 
 
  class MyWebViewClient extends WebViewClient{

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

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

         @Override
         public void onReceivedError(WebView view, int errorCode,
                 String description, String failingUrl) {
             Toast.makeText(MainActivity.this, "网页加载出错!", Toast.LENGTH_LONG);
            
             alertDialog.setTitle("ERROR");
             alertDialog.setMessage(description);
             alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                 public void onClick(DialogInterface dialog, int which) {
                     // TODO Auto-generated method stub
                 }
             });
             alertDialog.show();
         }
        
        
        
     }
    
     protected void initWebView(){
         //设计进度条
         progressBar = ProgressDialog.show(MainActivity.this, null, "正在进入网页,请稍后…");
         //获得WebView组件
         webView = (WebView) this.findViewById(R.id.webText);
        
         webView.getSettings().setJavaScriptEnabled(true);
        
      webView.getSettings().setDefaultTextEncodingName("gbk");
         webView.getSettings().setUseWideViewPort(true);
        
         webView.getSettings().setLoadWithOverviewMode(true);

   //webView.loadUrl("file:///android_asset/demo1.html");
         webView.loadUrl("http://wap.people.com.cn/");
   
         alertDialog = new AlertDialog.Builder(this).create();
        
         //设置视图客户端
         webView.setWebViewClient(new MyWebViewClient());
     }

 

 
 
 
 public boolean onKeyDown(int keyCode, KeyEvent event) {      
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {      
            webView.goBack();      
                   return true;      
        }      
        return super.onKeyDown(keyCode, event);      
    }    

  
}

转载于:https://www.cnblogs.com/wangheblog/archive/2013/04/23/3038726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值