Galaxy S4 上的浮动触控触控、Galaxy

2023-09-07 22:22:20 作者:__________、一抹。

Samsung Galaxy S4 具有Floating Touch"功能,即使不触摸屏幕也可以检测到手指.

Samsung Galaxy S4 have the "Floating Touch" functionality, in which the finger can be detected even if the screen is not touched.

当手指悬停时,我想在按钮 (btn1) 上触发一个事件.

I would like to fire an event on a button (btn1) when the finger is passing hover it.

我尝试使用 OnHoverListener,但是当 MotionEventMotionEvent.ACTION_HOVER_ENTERMotionEvent.ACTION_HOVER_EXITMotionEvent.ACTION_HOVER_MOVE,这是我需要的事件.

I tried using the OnHoverListener, but onHover never get called when the MotionEvent is MotionEvent.ACTION_HOVER_ENTER or MotionEvent.ACTION_HOVER_EXIT or MotionEvent.ACTION_HOVER_MOVE, which are the event that I need.

这是我的代码:

btn1.setOnHoverListener(new OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
    Log.d("FloatingTouch", "onHover: "+event.getAction());
    switch (event.getAction()) {
      case MotionEvent.ACTION_HOVER_ENTER:
          Log.d("FloatingTouch", "ACTION_HOVER_ENTER" );
          return true;
      case MotionEvent.ACTION_HOVER_MOVE:
          Log.d("FloatingTouch", "ACTION_HOVER_MOVE" );
          return true;
      case MotionEvent.ACTION_HOVER_EXIT:
          Log.d("FloatingTouch", "ACTION_HOVER_EXIT" );
          break;
      default:
          break;
    }
    return false;
}
});

我错过了什么吗?也许一些许可?

Do I miss something? Maybe some permission?

推荐答案

好的,我终于明白了!

要让它发挥作用,你必须:

To make it work you have to:

为清单中的 Activity 添加一个新的 Intent 过滤器到 com.sec.android.airview.HOVER:

<intent-filter>
<action android:name="com.sec.android.airview.HOVER" />
</intent-filter>

给你添加一个onHoverListener View

convertView.setOnHoverListener(new OnHoverListener() {
    @Override
    public boolean onHover(View v, MotionEvent event) {
        Log.d("ListAdapter", "Hover: " + item);
        return false;
    }
});

它会起作用的.