• 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);      
        }    

      
    }

  • 相关阅读:
    结对作业评分
    Week6&7——第一次项目冲刺(Alpha版本)
    软工辅修团队项目选题参考
    Week5——团队选题&需求分析
    Week4——结对练习&团队作业1
    nginx与Apache的对比以及优缺点
    oracle数据库-错误编码大全
    spring MVC原理
    Java操作Memcached
    zookeeper应用场景-java
  • 原文地址:https://www.cnblogs.com/wangheblog/p/3038726.html
Copyright © 2020-2023  润新知