首页 > Web开发 > 详细

10.3.3 WebView的几个常见功能

时间:2017-09-20 09:22:17      阅读:375      评论:0      收藏:0      [点我收藏+]

当前主流的开发模式是“WebView+ProgressDialog”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>
package com.example.progresstest;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WebViewActivity extends Activity {

    private ProgressDialog progressBar;
    private WebView webView;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        //加载WebView
        initWebView();
    }

    private void initWebView() {
        progressBar = ProgressDialog.show(WebViewActivity.this, null, "正在进入网页,请稍后....");
        //获得WebView组件
        webView = (WebView) this.findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        alertDialog = new AlertDialog.Builder(this).create();
        //设置视图客户端
        webView.setWebViewClient(new MyWebViewCLient());
    }
   @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
       if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
           webView.goBack();
           return true;
       }
    return super.onKeyDown(keyCode, event);
}
/*    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }*/
    class MyWebViewCLient extends WebViewClient {
             @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                 view.loadUrl(url);
                return true;
            }
             @Override
            public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
               if(progressBar.isShowing()){
                   progressBar.dismiss();
               }
            }
             @Override
            public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            // TODO Auto-generated method stub
            Toast.makeText(WebViewActivity.this, "网页加载出错!  ", Toast.LENGTH_LONG);
            alertDialog.setTitle("ERROR");
            alertDialog.setMessage(description);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    
                }
            });
            alertDialog.show();
             }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.progresstest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.progresstest.WebViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

 

技术分享技术分享

技术分享技术分享

技术分享

 

10.3.3 WebView的几个常见功能

原文:http://www.cnblogs.com/ZHONGZHENHUA/p/7559400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!