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