简体   繁体   English

webview,我怎样才能知道点击了哪个链接?

[英]webview, how can I find out which link was clicked in it?

I have a webview, how can I find out which link was clicked in this webview?我有一个 webview,我怎样才能知道在这个 webview 中点击了哪个链接?

Android/Java安卓/Java

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        tools:ignore="MissingConstraints">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <include
                android:id="@+id/back"
                layout="@layout/top_menu_back" />

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>
    <WebView
        android:id="@+id/newsdetail"
        android:layout_marginTop="60dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

hello, I have a webview, how can I find out which link was clicked in this webview?你好,我有一个webview,我怎样才能知道在这个webview中点击了哪个链接?

Just use this code snippet.只需使用此代码段。 :) :)

private class MyWebViewClient extends WebViewClient {
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                String link = url + "?header=0";
                Log.d("getUrl", link);
                view.loadUrl(link);
                return false;
            }
    
        }

Example:例子:

public class NewsDetail extends AppCompatActivity {

    WebView newsdetail;
    RelativeLayout navigation_icon;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_detail);

        navigation_icon = findViewById(R.id.navigation_icon);

        newsdetail = findViewById(R.id.newsdetail);
        newsdetail.loadUrl(getIntent().getStringExtra("newsdetail"));
        newsdetail.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = newsdetail.getSettings();
        webSettings.setJavaScriptEnabled(true);
        

    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            String link = url + "?header=0";
            Log.d("getUrl", link);
            view.loadUrl(link);
            return false;
        }

    }

}

Note: If the header parameter is allowed by the backend, it does not show the header in the application.注意:如果 header 参数被后端允许,它不会在应用程序中显示 header。 To be able to use it I guess it must also be allowed by the backend.为了能够使用它,我想后端也必须允许它。 Like the header=0 parameter, as in the example.如示例中的 header=0 参数一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能知道点击了哪个按钮? - How can I find out which button was clicked? Android如何找出列表视图中单击了哪个视图 - android how do i find out which view was clicked in a listview 如何找到我单击的日历按钮? - How can I find which calendar's button I clicked? 如何让我的应用程序获取在 WebView 中单击的链接的 URL,对其执行操作,然后加载它? - How can I get my app to get the URL of a link clicked in WebView, perform operations on it, and then load it? 如何找到Jsoup的POST链接? - How can I find out the link to POST to with Jsoup? 如何确定使用Java在JSP文件中单击了哪个链接? - How can I determine which link was clicked in my JSP file using Java? 我如何找出它是哪个图书馆包? - How can i find out which Library Package it is? Java,Mockito-如何找出调用了哪些模拟? - java, mockito - how can i find out which mocks are called? 在WebView中,如何知道单击了哪个按钮? - in WebView, how to know which button is clicked? 如何根据用户在一个活动中单击(三个)中的哪个按钮来更改单个WebView(Android)的内容? - How can I change the content of a single WebView (Android) depending on which button the user clicks (out of three) in one activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM