Android 悬停时按钮颜色更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10542451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:06:41  来源:igfitidea点击:

Button color change on hover

androidbuttoncolors

提问by Feona

I'm just a beginner with Android. I want the button to change its color on mouseover.
I don't know how to do that in android. Can it be done?

我只是一个 Android 初学者。我希望按钮在鼠标悬停时改变它的颜色。
我不知道如何在android中做到这一点。可以做到吗?

View for a button:

查看按钮:

<Button 
  android:id="@+id/b8"
  android:text="click me"
  style="?android:attr/buttonStyleSmall"
  android:textSize="20dp" />

采纳答案by Icemanind

You need to use what's called a selector.

您需要使用所谓的 a selector

You can read about them and get a tutorial from this site.

您可以阅读有关它们并从该站点获取教程

Keep in mind that there really isn't a concept in Android as "hover" since you can't hover your finger over the display. But you can create selectors for, say, when a button has focus. Normally a button can have three states: Normal, Focused and Pressed.

请记住,Android 中确实没有“悬停”的概念,因为您无法将手指悬停在显示屏上。但是您可以创建选择器,例如,当按钮具有焦点时。通常一个按钮可以有三种状态:正常、聚焦和按下。

回答by samreen

The particular mouse hover functionality as we know it, is supported on Android OS 4.0 and above. Views have onHoverListeners(). Isnt it finally great

我们所知的特定鼠标悬停功能在 Android OS 4.0 及更高版本上受支持。视图具有 onHoverListeners()。是不是终于很棒

回答by ShriKant A

Here is xml-- hover.xml --

这是 xml--hover.xml--

<item android:drawable="@drawable/image__hover" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/normalimage"/>

and how i use it in Button for color change on hover

以及我如何在 Button 中使用它在悬停时更改颜色

<Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/hover"
                        ></Button>

回答by linuxguy

Hovering is possible on Android: all devices that have a track ball or D-pad or QWERTY keyboard arrow keys can move the "hover" or focus to anything that is pressable (clickable). Then, on my G1 for example, you press the track ball to press it down into the android:state_pressedstate.

在 Android 上可以悬停:所有带有轨迹球、方向键或 QWERTY 键盘箭头键的设备都可以将“悬停”或焦点移动到任何可按下(可点击)的位置。然后,例如在我的 G1 上,您按下轨迹球将其按下进入android:state_pressed状态。