gpt4 book ai didi

android - 如何获取android webView中的触摸事件信息?

转载 作者:行者123 更新时间:2023-11-29 22:58:52 25 4
gpt4 key购买 nike

我试图弄清楚用户是否触摸了屏幕以及他按下了哪个元素并收集有关该元素的信息,例如如果用户按下由 anchor html 标记包裹的图像,那么我会尝试找出来源。

背景:

我们网站上有一个链接图片,它有一个相对 URL,例如

<a href="/products/product01.html`><img src="/media/images/image01.jpg"></a>

但是Android webview有一个bug,webview无法识别这个链接。因此,我需要通过监听用户触摸的所有内容来解决问题,确定它是否是一个链接,如果链接以斜杠 / 开头,则在其前面添加基本 URL。

我这样试过,但没有任何记录:

public class FullscreenActivity extends AppCompatActivity
{
private WebView blizzView;
private Button backButton;
private String website;

private View.OnTouchListener handleTouch = new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
Log.i("debug_log", "touch");

int x = (int) event.getX();
int y = (int) event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("debug_log", "touched down");
break;
case MotionEvent.ACTION_MOVE:
Log.i("debug_log", "moving: (" + x + ", " + y + ")");
break;
case MotionEvent.ACTION_UP:
Log.i("debug_log", "touched up");
break;
}

return true;
}
};
...

我当然过滤了 debug_log

enter image description here

最佳答案

我发现 webView 有一个名为 setOnTouchListener 的函数,我必须在其中添加逻辑:

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Message;
import android.webkit.URLUtil;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.util.Log;

/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class FullscreenActivity extends AppCompatActivity
{
private WebView webView;
private Button backButton;
private String website;

...

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

website = "https://www.example.com";
setContentView(R.layout.activity_fullscreen);
webView = findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {

webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

int x = (int) event.getX();
int y = (int) event.getY();

Log.i("debug_log", "moving: (" + x + ", " + y + ")");

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("debug_log", "touched down");
break;
case MotionEvent.ACTION_MOVE:
Log.i("debug_log", "moving: (" + x + ", " + y + ")");
break;
case MotionEvent.ACTION_UP:
Log.i("debug_log", "touched up");
break;
}

return false;
}
});

// URL laden:
webView.loadUrl(website);
}

...

关于android - 如何获取android webView中的触摸事件信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com