閱讀306 返回首頁    go iPhone_iPad_Mac_apple


android WebView 瀏覽曆史管理前進和後退


通過設置WebViewClient,可以設置在網頁開始下載和下載完畢後要做的操作。如在下載時進度條和下載時標題顯示文字可以在這設置,下載後設置title為網頁標題也要在這設置。開戶前進後退等功能也要在這設置。

下麵的程序中前進後退等按鈕的可用屬性是和WebVeiw可否前進後退是同步的。同時Activity兼具事件監聽器的功能。

package com.iteedu.webview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewExample extends Activity 
	implements OnClickListener, OnKeyListener {
	//設定WebViewClient的處理程序
	class MyWebViewClient extends WebViewClient {
		@Override
		public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
			back.setEnabled(webView.canGoBack());
			forward.setEnabled(webView.canGoForward());
		}
		@Override
		public void onPageFinished(WebView view, String url) {
	//設置程序的標題為網頁的標題
			if (webView.getTitle() != null) {
				WebViewExample.this.setTitle(webView.getTitle());
			}
		}
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			WebViewExample.this.setTitle("Loading...");
			back.setEnabled(webView.canGoBack());
			forward.setEnabled(webView.canGoForward());
		}
	}
	Button back;
	Button forward;
	Button first;
	Button last;
	Button clear;
	EditText url;
	WebView webView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.example03);
		//建立顯示畫麵上Button,EditText,WebView類的實例變量
		first = (Button)findViewById(R.id.Button01);
		back = (Button)findViewById(R.id.Button02);
		forward = (Button)findViewById(R.id.Button03);
		last = (Button)findViewById(R.id.Button04);
		clear = (Button)findViewById(R.id.Button05);
		url = (EditText)findViewById(R.id.EditText01);
		webView = (WebView)findViewById(R.id.WebView01);
		webView.setWebViewClient(new MyWebViewClient());
		//設定Button和EditText的監聽功能
		back.setOnClickListener(this);
		forward.setOnClickListener(this);
		first.setOnClickListener(this);
		last.setOnClickListener(this);
		clear.setOnClickListener(this);
		url.setOnKeyListener(this);
	}
	//按下Button時的處理程序
	public void onClick(View v) {
		if (v == back) {
			if (webView.canGoBack()){
				webView.goBack();
			}
		} else if (v == forward) {
			if (webView.canGoForward()){
				webView.goForward();
			}
		} else if (v == first) {
			if (webView.canGoBackOrForward(-2)){
				webView.goBackOrForward(-2);
			}
		} else if (v == last) {
			if (webView.canGoBackOrForward(+2)){
				webView.goBackOrForward(+2);
			}
		} else if (v == clear) {
				webView.clearHistory();
		}
	}
	//於url(EditText)編輯框按下ENTER時的處理程序,下載新的網頁
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_ENTER) {
			webView.loadUrl(url.getText().toString());
			return true;
		}
		return false;
	}
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:andro
	android:orientation="vertical">
	<LinearLayout android:
		android:layout_height="wrap_content" android:layout_width="fill_parent">
		<Button android: android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="<<" />
		<Button android: android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="<" />

		<Button android: android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text=">"></Button>
		<Button android: android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text=">>"></Button>
		<Button android: android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Clear History"></Button>
	</LinearLayout>
	<EditText android: android:layout_height="wrap_content"
		android:text="https://" android:layout_width="fill_parent"
		android:maxLines="1" />
	<WebView android: android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>


最後更新:2017-04-02 16:48:06

  上一篇:go svn對項目權限進行管理
  下一篇:go linux環境變量設置