代码之家  ›  专栏  ›  技术社区  ›  Mike

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

  •  0
  • Mike  · 技术社区  · 7 年前

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

    这是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"
        />
    

    网页加载良好。但是没有导航属性,我也希望能够“取消”Web视图并返回到父活动。

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

    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 回复  |  直到 7 年前
        1
  •  1
  •   Amad Yus    7 年前

    您应该有工具栏,因为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自定义选项卡数量

        2
  •  1
  •   Tobias Uhmann    7 年前

    我猜“导航属性”指的是浏览器chrome,即通常的后退和前进按钮、刷新/取消按钮和URL栏。这些在WebView中不可用。

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

    关于“取消”WebView:您可以覆盖活动的 onBackPressed 像这样:

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

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

        3
  •  1
  •   Upendra Shah    7 年前

    是的,WebView没有导航属性,但您可以处理 goBack ,则, goForward reload

    对于 戈巴克

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

    对于 goForward前进

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

    对于 重新加载

     myWebView.reload();