1. 程式人生 > >10.3.3 WebView的幾個常見功能

10.3.3 WebView的幾個常見功能

layout class alert 組件 creat mage ets error settitle

當前主流的開發模式是“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的幾個常見功能