首页 > 解决方案 > Android Studio:单击网页视图中的超链接后如何打开新的活动?

问题描述

这些天我为我的学校构建了一个应用程序,一个抽屉片段包含一个带有学校新闻的 web 视图。文章下方的按钮可在单击它们后打开包含完整文章的新链接。(如“阅读更多” - 按钮)。

我想在单击它们后打开一个新活动,与单击哪篇文章无关,因为我不需要为每篇新闻文章创建一个活动。

我希望你能理解我的问题并给我一个解决方案。谢谢

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@android:color/white">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>





<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>






    static WebView webview;
    private View mContentView;
    SwipeRefreshLayout swipe;





    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_news, container, false);





        swipe = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                loadWeb(v);

            }
        });

        loadWeb(v);
        return v;



    }






    private void webViewGoBack(){
        webview.goBack();
    }





    public void loadWeb(final View v) {
        webview = (WebView) v.findViewById(R.id.webView);
        WebSettings webSettings = webview.getSettings();
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.getSettings().setAppCacheEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY );
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);


        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.setLayerType(WebView.LAYER_TYPE_NONE, null);
        swipe.setRefreshing(true);
        webview.setWebViewClient(new WebViewClient() {


            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                swipe.setRefreshing(false);


                {

                    webview.loadUrl
                            ("javascript:(function() { " +
                                    "document.getElementsByTagName('header')[0].style.display='none'; " +
                                    "document.getElementsByTagName('nav')[0].style.display='none'; " +
                                    "document.getElementsByTagName('footer')[0].style.display='none'; " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_search')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_recent_entries')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_archive')[0].remove(); " +
                                    "document.getElementsByClassName('cat-item cat-item-3')[0].remove(); " +
                                    "document.getElementsByClassName('l-header pos_fixed bg_solid shadow_wide')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('grecaptcha-badge')[0].remove(); " +
                                    "document.getElementsByClassName('rc-anchor-logo-img rc-anchor-logo-img-large')[0].remove(); " +


                                    "})()");

                    webview.setVisibility(View.VISIBLE);


                }
            }








        });

        webview.loadUrl("https://www.bbs-varel.de/aktuelles/");
        webview.setOnKeyListener(new View.OnKeyListener(){




            public boolean onKey(View v, int keyCode, KeyEvent event) {




                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == MotionEvent.ACTION_UP
                        && webview.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }

                return false;
            }

        });





    }



    }

标签: android

解决方案


有一个办法就是。简而言之,它包括向 webview 注册一个侦听器,以便首先将每个页面导航(即单击按钮时加载的新页面)传递给您。

去做这个:

Webview webview = findViewById(R.id.webView)
webView.setWebViewClient(webViewClient); 
webView.setWebChromeClient(new WebChromeClient());

你的班级在哪里webViewClient实施android.webkit.WebViewClient. 在那里,您会找到一种方法android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String),它可以让您“挂钩”来检查用户是否单击了您感兴趣的内容。如果返回 true,则不会加载新页面。如果您返回 false,则页面的行为就像没有附加侦听器一样。祝你好运!


推荐阅读