开发者

Show affordance/hover/tooltip on a long-clicked View

开发者 https://www.devze.com 2023-02-11 19:55 出处:网络
I would like to show a tooltip, i.e. additional non-essential information开发者_Python百科 about a View when the user long-clicks on it.

I would like to show a tooltip, i.e. additional non-essential information开发者_Python百科 about a View when the user long-clicks on it.

The two options I see in front of me are using an OnLongClickListener to construct a custom tooltip in front of the clicked View; or abusing an OnCreateContextMenuListener to create a context menu that isn't.

Neither seems like the best way to go about things, and I'm not sure whether either will work. I've scoured the web and haven't found any hints. Any alternatives, or should I be wet-fish-slapped for trying to do this? Thanks!


Android Oreo has introduced the android:tooltipText attribute in order to display a simple Toast-like tooltip when user long-presses on a View:

<Button
    // ...
    android:tooltipText="@string/share_button_tooltip"/>

Although it has been introduced in API 26, you can still use it through the Support Library's TooltipCompat helper class:

TooltipCompat.setTooltipText(shareButton, getString(R.string.share_button_tooltip))

My suggestion is to set android:contentDescription and then use it as the tooltip text to kill 2 stones with 1 bird:

<Button
    // ...
    android:contentDescription="@string/share_button_tooltip"/>

TooltipCompat.setTooltipText(shareButton, shareButton.getContentDescription())
0

精彩评论

暂无评论...
验证码 换一张
取 消