主布局文件:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <LinearLayout android:orientation="horizontal" 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content"> 9 <Button android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:id="@+id/bt_back" 12 android:text="后退"/> 13 <Button android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/bt_forward" 16 android:text="前进"/> 17 <EditText android:layout_width="150px" 18 android:layout_height="wrap_content" 19 android:id="@+id/ed_url" 20 android:singleLine="true" 21 android:hint="请输入网址"/> 22 <Button android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:id="@+id/bt_go" 25 android:text="GO"/> 26 </LinearLayout> 27 <WebView android:layout_width="fill_parent" 28 android:layout_height="fill_parent" 29 android:id="@+id/webview"/> 30</LinearLayout>
主活动类WebViewMainActivity.java:
1package com.example.ch10; 2 3import com.example.baseexample.R; 4 5import android.app.Activity; 6import android.os.Bundle; 7import android.view.KeyEvent; 8import android.view.View; 9import android.view.Window; 10import android.webkit.URLUtil; 11import android.webkit.WebChromeClient; 12import android.webkit.WebSettings; 13import android.webkit.WebView; 14import android.webkit.WebViewClient; 15import android.widget.Button; 16import android.widget.EditText; 17import android.widget.Toast; 18 19public class WebViewMainActivity extends Activity { 20 private WebView webview ; 21 private Button bt_back,bt_forward,bt_go; 22 private EditText ed_url; 23 24 public void onCreate(Bundle savedInstanceState){ 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.ch10webviewmain); 27 28 webview = (WebView)findViewById(R.id.webview); 29 ed_url = (EditText)findViewById(R.id.ed_url); 30 bt_back = (Button)findViewById(R.id.bt_back); 31 bt_forward = (Button)findViewById(R.id.bt_forward); 32 bt_go = (Button)findViewById(R.id.bt_go); 33 34 WebSettings webSettings = webview.getSettings(); 35 webSettings.setBuiltInZoomControls(true); 36 webSettings.setDefaultFontSize(9); 37 webSettings.setAllowFileAccess(true); 38 webview.setWebViewClient(new WebViewClient(){ 39 public boolean shouldOverrideUrlLoading(WebView view,String url){ 40 view.loadUrl(url); 41 return true; 42 } 43 }); 44 45 webview.setWebChromeClient(new WebChromeClient(){ 46 public void onReceivedTitle(WebView view,String title){ 47 WebViewMainActivity.this.setTitle(title); 48 super.onReceivedTitle(view, title); 49 } 50 public void onProgressChanged(WebView view,int newProgress){ 51 WebViewMainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100); 52 super.onProgressChanged(view, newProgress); 53 } 54 }); 55 56 bt_go.setOnClickListener(new Button.OnClickListener(){ 57 58 @Override 59 public void onClick(View v) { 60 String url = ed_url.getText().toString().trim(); 61 if(URLUtil.isNetworkUrl(url)){ 62 webview.loadUrl(url); 63 }else{ 64 Toast.makeText(WebViewMainActivity.this, "网址错误", Toast.LENGTH_LONG).show(); 65 } 66 } 67 68 }); 69 70 bt_back.setOnClickListener(new Button.OnClickListener(){ 71 72 @Override 73 public void onClick(View arg0) { 74 if(webview.canGoBack()){ 75 webview.goBack(); 76 } 77 } 78 79 }); 80 81 bt_forward.setOnClickListener(new Button.OnClickListener(){ 82 83 @Override 84 public void onClick(View arg0) { 85 86 if(webview.canGoForward()){ 87 webview.goForward(); 88 } 89 } 90 91 }); 92 93 } 94 95 public boolean onKeyDown(int keyCode,KeyEvent event){ 96 if((keyCode==KeyEvent.KEYCODE_BACK) && webview.canGoBack()){ 97 webview.goBack(); 98 return true; 99 }else if(keyCode==KeyEvent.KEYCODE_BACK){ 100 return super.onKeyDown(keyCode, event); 101 } 102 return false; 103 } 104 105}