سؤال

I have a question about the MotionEvent in Android. I want to sent a "Click" to a Webview because there is an iframe and i have to click an Image/Button, which is in the iframe. So I can´t you js because I can not control the content of the iframe.

My Code:

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);
}

And in 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);
    }
   });

The webView Load the Url, but the Code does not Click on it. If I click on the webView the onTouch Event is fired.

Can you please help me?

Best greetings form Germany.

هل كانت مفيدة؟

المحلول

Pass the webview as argument to your simulateDoubleTapEvent() method and use it for dispatching.

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top