gpt4 book ai didi

Android:MotionEvent.obtain 与 webview 不工作

转载 作者:行者123 更新时间:2023-11-30 03:12:46 26 4
gpt4 key购买 nike

我对 Android 中的 MotionEvent 有疑问。我想向 Webview 发送“点击”,因为有一个 iframe,我必须单击 iframe 中的图像/按钮。所以我不能控制 js,因为我无法控制 iframe 的内容。

我的代码:

private void simulateDoubleTapEvent(int action)
{
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 35.407104f;
float y = 378.52066f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent me = MotionEvent.obtain(
downTime,
eventTime,
action,
x,
y,
metaState
);
dispatchTouchEvent(me);
}

在 onCreate 中:

webview2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
event.getMetaState();
Log.v("ON_TOUCH", "Action = " + action + " View:" + v.toString());
Log.v("ON_TOUCH", "X = " + x + "Y = " + y);


return false;
}
});
webview2.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{ view.loadUrl(url); return true; }
public void onPageFinished(WebView view, String url) {
Log.w("webview2","Load");

simulateDoubleTapEvent(0);
simulateDoubleTapEvent(1);
}
});

webView 加载了 Url,但是 Code 没有点击它。如果我单击 webView,则会触发 onTouch 事件。

你能帮帮我吗?

来自德国的问候。

最佳答案

将 webview 作为参数传递给您的 simulateDoubleTapEvent() 方法并将其用于调度。

private void simulateDoubleTapEvent(WebView view, int action)
{
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 35.407104f;
float y = 378.52066f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent me = MotionEvent.obtain(
downTime,
eventTime,
action,
x,
y,
metaState
);
view.dispatchTouchEvent(me);
}

关于Android:MotionEvent.obtain 与 webview 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660940/

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