我使用了一些 WebView 类的方法来执行以下功能。

loadUrl():从 url 加载网页

canGoBack():检查前一页历史记录是否可用

goBack():返回上一页

canGoForward():检查下一页历史记录是否可用

goForward():转到下一页

reload():重新加载当前页面

stopLoading():停止加载当前页面

 

此浏览器将允许您执行以下操作。

  • 打开网页网址。
  • 取消加载网页。
  • 转到上一个或下一个网页。
  • 重新加载网页。
  • 在进度条中查看网页的加载进度。

 

创建一个包名为com.androidbrowser的 android 项目,并在各自的文件中添加以下代码。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:tools="http://schemas.android/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:indeterminate="false"
        android:max="100"
        android:progress="1"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_below="@+id/progress">

        <EditText
            android:id="@+id/urlBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Type URL and Press Enter..."
            android:imeOptions="actionSearch"
            android:paddingLeft="5dp"
            android:singleLine="true" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="x"
            android:visibility="visible" />
    </RelativeLayout>

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomBar"
        android:layout_below="@+id/topBar" />

    <RelativeLayout
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:gravity="center">

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:text="Back" />

        <Button
            android:id="@+id/forward"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_toRightOf="@+id/back"
            android:text="Forward" />

        <Button
            android:id="@+id/refresh"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/forward"
            android:text="Reload" />
    </RelativeLayout>
</RelativeLayout>

MainActivity.java

package com.androidbrowser;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;


public class MainActivity extends Activity {
    EditText urlBox;
    WebView webView;
    Button back, forward, refresh, cancel;
    ProgressBar progress;

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

        urlBox = (EditText) findViewById(R.id.urlBox);
        webView = (WebView) findViewById(R.id.webView);
        back = (Button) findViewById(R.id.back);
        forward = (Button) findViewById(R.id.forward);
        refresh = (Button) findViewById(R.id.refresh);
        cancel = (Button) findViewById(R.id.cancel);
        progress = (ProgressBar) findViewById(R.id.progress);

        webView.setWebViewClient(new CustomWebViewClient());
        webView.setWebChromeClient(new CustomWebChromeClient());

        urlBox.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //when enter is pressed in edittext, start loading the page
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    webView.loadUrl(urlBox.getText().toString());
                    return true;
                }
                return false;
            }
        });

        //go to previous page
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                }
            }
        });

        //go to next page
        forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoForward()) {
                    webView.goForward();
                }
            }
        });

        //reload page
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.reload();
            }
        });

        //cancel loading page
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.stopLoading();
            }
        });
    }


    public class CustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public class CustomWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progress.setProgress(newProgress);
            urlBox.setText(view.getUrl());

            if (newProgress == 100) {
                cancel.setVisibility(View.GONE);
                progress.setVisibility(View.GONE);


            } else {
                cancel.setVisibility(View.VISIBLE);
                progress.setVisibility(View.VISIBLE);

            }
        }
    }
}

注意:确保添加 Internet 访问权限。只需在AndroidManifest.xml文件中添加以下行。

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android/apk/res/android"
    package="com.androidbrowser">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AndroidBrowser"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AndroidBrowser.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

就是这样,你已经做到了。只需运行该项目并享受浏览互联网。

 

输出

 

更多推荐

如何在 Android 中制作简单的浏览器