2013-10-28 46 views
4

我有一個webview,我正在設置onTouchListener,以捕獲實際視圖上的滑動事件。Android將觸摸事件傳遞給WebView的內容

我還在WebView上設置了一個WebViewClient,以便在點擊webview中的某些鏈接時覆蓋Url Loading。

問題是OnTouch處理程序總是首先收到動作,即使我返回false(當不是滑動),它不會將觸摸事件傳遞給內部html內容,因此鏈接永遠不會實際單擊。如果我刪除onTouchListener它工作正常。是否有可能以某種方式將觸摸事件傳遞給內容?

回答

4

您需要設置,主要活動的OnCreate(),一個OnTouchListener()與requestFocus()方法中:

mWebView = (MyWebView) findViewById(R.id.webview); 
    mWebView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
         case MotionEvent.ACTION_DOWN: 
         case MotionEvent.ACTION_UP: 
          if (!v.hasFocus()) { 
           v.requestFocus(); 
          } 
          break; 
        } 
        return false; 
       } 
     }); 
+0

它不適合我.... https://view.officeapps.live.com/op/view.aspx?src = http://bvcelearningadmin.in/assets/upload_data/topic_files/electrician/ d84a3739e232baeb8606ba10738d642a.pptx ...請幫我先生 –

0

不要使用OnTouchListener,而是在你的WebView覆蓋的onTouchEvent:

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    // do your stuff here... the below call will make sure the touch also goes to the webview. 

    return super.onTouchEvent(event); 
} 
+0

當我加載webview的時候只有它的working.after加載webview wh我觸摸它不工作.... https://view.officeapps.live.com/op/view.aspx?src = http://bvcelearningadmin.in/assets/upload_data/topic_files/electrician/d84a3739e232baeb8606ba10738d642a.pptx。 ..請幫助我先生 –