2012-09-16 154 views

回答

1
Webview.setWebViewClient(new WebViewClient(){ 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) {  
        return false; 
     } 
}); 
+0

這對停止用戶點擊WebView沒有任何影響。這只是讓我的應用程序導航到鏈接,而無需轉到默認瀏覽器。 – elude

+1

我認爲答案是正確的,除非你想返回** true **而不是false,表明你將在你的主機應用程序中處理URL加載。 – Maks

+0

返回true將僅打開默認瀏覽器中按下的任何鏈接。 – elude

2

下面的代碼允許您滾動,但點擊鏈接時不會進入下一頁。

webView.setLongClickable(false); 

webView.setOnTouchListener(new View.OnTouchListener() 
{ 

    @Override 

    public boolean onTouch(View arg0, MotionEvent arg1) 
    { 

     return arg1.getAction() == MotionEvent.ACTION_UP; 

    } 
} 

); 
1

我用rattisuk的答案在拳頭,但意識到,如果你點擊某個地方,點擊被截獲,但變成了長按,使手機震動,並坡平一些潛在的編輯菜單,如果你點擊一些文本字段。 此外,無論何時按下webview中的可變UI元素(例如單擊鏈接或按鈕),這些UI元素都會改變外觀。 爲了防止這種情況,這是我的改進方案:

// Disable the haptic feedback so that handling the long click doesnt make the phone vibrate 
webview.setHapticFeedbackEnabled(false); 
// Intercept long click events so that they dont reach the webview 
webview.setOnLongClickListener(new OnLongClickListener() { 
    @Override 
    public boolean onLongClick(final View v) { 
     return true; 
    } 
}); 
// Intercept any touch event not related to scrolling/zooming 
webview.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(final View v, final MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      // Change ACTION_DOWN location to outside of the webview so that it doesnt affect 
      // pressable element in the webview (buttons receiving PRESS" will change appearance) 
      event.setLocation(webview.getWidth() + 1, webview.getHeight() + 1); 
     } 
     // Intercept any "up" event 
     return event.getAction() == MotionEvent.ACTION_UP; 
    } 
}); 
+0

謝謝@androidseb它幫助我禁用所有觸摸功能,但滾動 – Reshma

0

這裏是我的解決方案。它不會讓用戶觸發JavaScript點擊事件

public class NonClickableWebView extends WebView { 
    private GestureDetectorCompat gestureDetector; 
    private boolean isSingleTap = false; 

    public NonClickableWebView(Context context) { 
     super(context); 
     gestureDetector = new GestureDetectorCompat(context, new GestureDetector()); 
    } 

    public NonClickableWebView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     gestureDetector = new GestureDetectorCompat(context, new GestureDetector()); 
    } 

    public NonClickableWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     gestureDetector = new GestureDetectorCompat(context, new GestureDetector()); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      isSingleTap = false; 
     } 
     gestureDetector.onTouchEvent(event); 

     if (isSingleTap && event.getAction() == MotionEvent.ACTION_UP) { 
      return true; 
     } 

     return super.onTouchEvent(event); 
    } 

    private class GestureDetector extends android.view.GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onSingleTapUp(MotionEvent e) { 
      isSingleTap = true; 
      return false; 
     } 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 
      isSingleTap = true; 
      return false; 
     } 
    } 
}