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

时间:2018-03-07 08:06:46

标签: android android-webview

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

这是webview布局:

<?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"
    />

网页加载正常。但是没有导航属性,我也希望能够取消&#34;取消&#34; WebView并返回父活动。

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

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

活性;

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);
    }
}

但是我无法看到任何导航属性,而且我也不确定如何取消活动,因为我已将应用栏移除为主题。

3 个答案:

答案 0 :(得分:1)

我想用&#34;导航属性&#34;你的意思是浏览器镶边,即通常的后退和前进按钮,刷新/取消按钮和URL栏。这些在WebView中不可用。

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

关于&#34;取消&#34; WebView:您可以像这样覆盖活动onBackPressed

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

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

答案 1 :(得分:1)

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

goBack

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

goForward

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

reload

 myWebView.reload();

答案 2 :(得分:1)

您应该有工具栏,因为webview不提供任何导航按钮。您可以在布局中使用这样的内容,并使用onBackPressed设置后退按钮操作。您需要覆盖onBackPressed并检查您的网络视图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自定义标签