【问题标题】:prevent double tap on android WebView from reaching html but let tap reach html防止在android WebView上双击到达html,但让点击到达html
【发布时间】:2021-11-10 20:40:24
【问题描述】:

我正在尝试使用 android webView 创建一个混合应用程序。
这是我的问题:
我在 webView 上有一个 webView 和一个 touchListener。我希望点击事件下降到 html 但阻止 doubleTap 从到达 webview 内部的 html。
这是我的代码:
webView.setOnTouchListener { v, event ->
    //returning true here prevents all kind of touches from reaching html
    true
 }

上面的代码阻止了所有的触摸到达 html。 我想要这样的东西:

webView.setOnTouchListener { v, event ->
        if (doubleTap) {
            //return true and prevent doubleTap from reaching html
            true
        } else (!doubleTap) {
            //return false and let html recieve tap or any other gesture!
            false
        }
    }

顺便说一句,我不想​​用 JavaScript 或我的 html 页面中的任何东西来处理这个问题! (告诉我代码是否很短,您需要更多信息) 谢谢大家!

【问题讨论】:

    标签: android kotlin touch-event


    【解决方案1】:

    如果您想在双击时识别它们,我建议您使用GestureDetector

    它有一个特定的双击监听器,称为 GestureDetector.OnDoubleTapListener

    如何使用 GestureDetector 的示例如下(取自documentation):

    class MainActivity :
        Activity(),
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {
    
    private lateinit var mDetector: GestureDetectorCompat
    
    // Called when the activity is first created.
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Instantiate the gesture detector with the
        // application context and an implementation of
        // GestureDetector.OnGestureListener
        mDetector = GestureDetectorCompat(this, this)
        // Set the gesture detector as the double tap
        // listener.
        mDetector.setOnDoubleTapListener(this)
    }
    
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return if (mDetector.onTouchEvent(event)) {
            true
        } else {
            super.onTouchEvent(event)
        }
    }
    
    override fun onLongPress(event: MotionEvent) {
        Log.d(DEBUG_TAG, "onLongPress: $event")
    }
    
    
    override fun onDoubleTap(event: MotionEvent): Boolean {  // <----
        Log.d(DEBUG_TAG, "onDoubleTap: $event")
        return true
    }
    
    override fun onDoubleTapEvent(event: MotionEvent): Boolean {  // <-----
        Log.d(DEBUG_TAG, "onDoubleTapEvent: $event")
        return true
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2022-09-30
      • 1970-01-01
      • 2018-07-31
      • 2022-08-13
      • 1970-01-01
      相关资源
      最近更新 更多