WebView混合開發模式二(實現網頁的放大,輸小等功能)

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"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章