阻止ImageButton工具提示在片段上悬停出现

问题描述

目前,我观察到一种奇怪的行为,即“通过”片段显示工具提示,并且我不知道如何消除它。任何想法或提示将不胜感激!

我有一个ImageButton和一些contentDescription

        <androidx.appcompat.widget.AppCompatimageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Content description"
            android:src="@drawable/ic_image"
            />

这样做是为了允许用户在将Stylus / S笔或鼠标指针悬停在图像上时看到帮助文本。这是理想的行为:

Normal behavior

在活动上方显示AlertDialog时,不会显示任何工具提示。这也是预期的行为:

No tooltip over AlertDialog

但是,当片段显示ImageButton上方时,总是显示工具提示。这是不希望的行为,也是这个问题的极大奥秘。片段具有半透明的绿色背景:

Tooltip over the fragment

片段布局中使用了一个简单的FrameLayout

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B94EC14A"
    />

用户可以点击该片段。

如果添加了这些行,则可以摆脱“点击进入”行为,而不能摆脱“悬停通过”行为:

        layout1.setClickable(true);
        layout1.setFocusable(true);

        layout1.setonTouchListener((v,event) -> true);
        layout1.setonHoverListener((v,event) -> true);
        layout1.setonGenericMotionListener((v,event) -> true);

似乎在这里使用了其他事件。

  • 哪种事件用于通过视图转换“悬停”事件?
  • 如何防止此工具提示出现? (需要与AlertDialog类似的行为)

解决方法

不幸的是,我找不到问题的原因,所以我最终以一个对话框的形式开始了一个片段:

MyFragment fragment = new MyFragment();
...
fragment.setArguments(bundle);
fragment.setCancelable(false);
fragment.show(getSupportFragmentManager(),"dialog");

或者对于不同的用例,将片段打包为一个空的活动并开始该活动:

Intent intent = new Intent(this,MyActivity.class);
intent.putExtra(...);
startActivityForResult(intent,REQUEST_CODE);

在这种情况下(如问题所述),工具提示不会显示在悬停上。