2013-12-18 103 views
1

我有一個關於MotionEvent Android中的問題。我要送,因爲有一個iframe,我必須單擊圖像/按鈕,這是在iframe「咔嗒」的網頁視圖。所以我不能,你的js,因爲我無法控制iframe的內容。安卓:MotionEvent.obtain用的WebView不工作

我的代碼:

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

web視圖加載的URL,但代碼不點擊它。如果我點擊webView,觸發onTouch事件。

你能幫我嗎?

最佳問候形成德國。

回答

1

傳遞web視圖作爲參數傳遞給您的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); 
} 
+0

謝謝!!!!有用! :) – user3108818