【问题标题】:Detect ongoing touch event检测正在进行的触摸事件
【发布时间】:2014-10-09 19:46:53
【问题描述】:

我目前正在尝试在我的 Android 应用中检测 正在进行的触摸事件。 具体来说,我想在片段中识别用户是否触摸了应用屏幕的任何部分。

Android 的OnTouchListener 按预期工作,除非触摸事件持续时间超过几秒没有移动

例如,前 1-2 秒的触摸会被检测到,但之后的一切都不会。

是否有类似“OnPressListener”或解决方法?

【问题讨论】:

  • 您需要长按监听器吗?
  • 是的,类似的

标签: android touch android-event


【解决方案1】:

如果您要检测的是定义明确的手势,请查看 GestureDetector。

http://developer.android.com/reference/android/view/GestureDetector.html

【讨论】:

    【解决方案2】:

    你可以使用

       aButton.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View arg0) {
            Toast.makeText(getApplicationContext(), "Long Clicked " ,
                  Toast.LENGTH_SHORT).show();
    
            return true;    // <- set to true
        }
        });
    

    在您的 aButton 上,如果您使用 API

    android:longClickable="true"
    

    布局xml中的aButton属性。

    【讨论】:

    • 感谢您的回答,但只要触摸持续,我就需要获取事件,而不仅仅是触摸发生后的事件。
    【解决方案3】:

    我终于找到了。

    解决方法是使用简单的OnTouchListener

    private boolean pressed = false;
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            pressed = true;
    
        } else if ((action == MotionEvent.ACTION_UP)
                || (action == MotionEvent.ACTION_CANCEL)) {
            pressed = false;
        }
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      相关资源
      最近更新 更多