Andorid WebView 瀏覽網頁

Andorid WebView 瀏覽網頁

這裡寫圖片描述

<uses-permission android:name="android.permission.INTERNET" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="shortcut.song.com.myapplication.WebViewTestActivity">
    <EditText
        android:id="@+id/url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="search"
            android:onClick="doSearch"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Back"
            android:onClick="goBack"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forward"
            android:onClick="goForward"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ZoomIn"
            android:onClick="doZoomin"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ZoomOut"
            android:onClick="doZoomout"/>
    </LinearLayout>
    <WebView
        android:id="@+id/web_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
package shortcut.song.com.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;

public class WebViewTestActivity extends AppCompatActivity {

    EditText editTextUrl;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_test);

        editTextUrl = (EditText)findViewById(R.id.url);
        webView = (WebView)findViewById(R.id.web_show);

    }

    public void doSearch(View v) {
        webView.loadUrl(editTextUrl.getText().toString());
    }

    public void goBack(View v) {
        webView.goBack();
    }

    public void goForward(View v) {
        webView.goForward();
    }

    public void doZoomin(View v) {
        webView.zoomIn();
    }

    public void doZoomout(View v) {
        webView.zoomOut();
    }

}

運行效果:

這裡寫圖片描述

You May Also Like