为什么我在 android 中使用 WebViewClient 时无法导航?

Why can't I navigate when using a WebViewClient in android?

我创建了一个 Activity 网站启动。 在应用程序中,我已将 AppTheme 设置为 Theme.AppCompat.NoActionBar 因此操作栏不会显示在任何 activity 布局上。

这是网络视图布局:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

网页加载正常。但是没有导航属性,我也希望能够 "cancel" WebView 并返回父级 activity。

我尝试实现我在 android 开发页面上找到的一些代码,特别是让网页在 WebView 中打开,而不是在浏览器中打开,例如。 chrome.

https://developer.android.com/guide/webapps/webview.html

Activity;

public class WebActivity extends AppCompatActivity {

    String webAddress;
    WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        myWebView = (WebView) findViewById(R.id.webview);
        webAddress = getIntent().getStringExtra("WEB_ADDRESS");
        myWebView.loadUrl(webAddress);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

但是我看不到任何导航属性,而且我也不确定如何取消 activity,因为我已经删除了作为主题的应用栏。

我猜 "navigation properties" 是指浏览器 chrome,即通常的后退和前进按钮、refresh/cancel 按钮和 URL 栏。这些在 WebView 中不可用。

您可以实现 UI 调用适当方法的小部件,例如 WebView.goBack()WebView.loadUrl() 等。但是如果您需要,最好通过 intent 调用真实的浏览器.

关于 "canceling" WebView:您可以像这样覆盖 activity 的 onBackPressed

@Override
public void onBackPressed() {
    if (webView.canGoBack())
        webView.goBack();
    else
        super.onBackPressed();
}

效果是按下设备上的后退按钮会返回浏览器历史记录,直到到达初始网页。然后它像往常一样销毁 activity 和 returns 到父级 activity.

是的,WebView 没有导航属性,但您可以处理 goBackgoForwardreload

对于goBack

 if (myWebView.canGoBack()) {
      myWebView.goBack();
 }

对于goForward

 if (myWebView.canGoForward()) {
      myWebView.goForward();
 }

对于reload

 myWebView.reload();

您应该有工具栏,因为 webview 不提供任何导航按钮。你可以在你的布局中有这样的东西,并用 onBackPressed 设置后退按钮动作。您需要覆盖 onBackPressed 并检查您的 webview canGoBack().

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:contentInsetStart="5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageButton
                    android:id="@+id/backBtn"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/ic_arrow_back_white_24dp"
                    android:layout_centerVertical="true"
                    android:background="?attr/selectableItemBackground"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_toRightOf="@+id/backBtn"
                    android:text="Your title"
                    android:textSize="22sp"
                    android:gravity="center_vertical"
                    android:textColor="@android:color/white"
                    android:ellipsize="end" />

            </RelativeLayout>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

或者,您也可以使用 chrome 自定义标签(适用于 Jellybean 及更高版本)。实现非常简单。请在此处查看 implementation guide 的 Chrome 个自定义选项卡