package com.example.webview_backforward;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button buttonback,buttonforward,ZoonmIn,ZoomOut;
private WebView webview;
//private static String URL="http://www.sina.com";
private static String URL="http://www.hao123.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonback=(Button)findViewById(R.id.button1);
buttonforward=(Button)findViewById(R.id.button2);
ZoonmIn=(Button)findViewById(R.id.button3);
ZoomOut=(Button)findViewById(R.id.button4);
webview=(WebView)findViewById(R.id.webView1);
buttonback.setOnClickListener(this);
buttonforward.setOnClickListener(this);
ZoonmIn.setOnClickListener(this);
ZoomOut.setOnClickListener(this);
webview.loadUrl(URL);
// webview.clearView();
}
@SuppressLint("NewApi") @Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
if(webview.canGoBack()){
webview.goBack();
}
break;
case R.id.button2:
if(webview.canGoForward()){
webview.goForward();
}
break;
case R.id.button3:
if(webview.canZoomIn()){
webview.zoomIn();
}
break;
case R.id.button4:
if(webview.canZoomOut()){
webview.zoomOut();
}
break;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="后退" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="前进" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="放大" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="输小" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginLeft="18dp"
android:layout_marginTop="116dp" />
</RelativeLayout>
访问网络需要加权限
<uses-permission android:name="android.permission.INTERNET"/>