主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_back"
android:text="后退"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_forward"
android:text="前进"/>
<EditText android:layout_width="150px"
android:layout_height="wrap_content"
android:id="@+id/ed_url"
android:singleLine="true"
android:hint="请输入网址"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_go"
android:text="GO"/>
</LinearLayout>
<WebView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview"/>
</LinearLayout>
主活动类WebViewMainActivity.java:
package com.example.ch10;
import com.example.baseexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class WebViewMainActivity extends Activity {
private WebView webview ;
private Button bt_back,bt_forward,bt_go;
private EditText ed_url;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch10webviewmain);
webview = (WebView)findViewById(R.id.webview);
ed_url = (EditText)findViewById(R.id.ed_url);
bt_back = (Button)findViewById(R.id.bt_back);
bt_forward = (Button)findViewById(R.id.bt_forward);
bt_go = (Button)findViewById(R.id.bt_go);
WebSettings webSettings = webview.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDefaultFontSize(9);
webSettings.setAllowFileAccess(true);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
});
webview.setWebChromeClient(new WebChromeClient(){
public void onReceivedTitle(WebView view,String title){
WebViewMainActivity.this.setTitle(title);
super.onReceivedTitle(view, title);
}
public void onProgressChanged(WebView view,int newProgress){
WebViewMainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
super.onProgressChanged(view, newProgress);
}
});
bt_go.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
String url = ed_url.getText().toString().trim();
if(URLUtil.isNetworkUrl(url)){
webview.loadUrl(url);
}else{
Toast.makeText(WebViewMainActivity.this, "网址错误", Toast.LENGTH_LONG).show();
}
}
});
bt_back.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
if(webview.canGoBack()){
webview.goBack();
}
}
});
bt_forward.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
if(webview.canGoForward()){
webview.goForward();
}
}
});
}
public boolean onKeyDown(int keyCode,KeyEvent event){
if((keyCode==KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
webview.goBack();
return true;
}else if(keyCode==KeyEvent.KEYCODE_BACK){
return super.onKeyDown(keyCode, event);
}
return false;
}
}