Android 按钮悬停事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11272621/
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
Android button hover event
提问by Bruno Bieri
I use a XML file to display a button with 2 states (normal, pressed). How can I find out when the button is pressed? Is there an event like onClick --> onPressed or how can I find it out?
我使用 XML 文件来显示具有 2 个状态(正常、按下)的按钮。如何知道按钮何时被按下?是否有像 onClick --> onPressed 这样的事件,或者我怎样才能找到它?
Here is the XML file I use:
这是我使用的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/buttonbluepressed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
<item android:drawable="@drawable/buttonblue"/>
</selector>
And here is how I use it:
这是我如何使用它:
<Button
android:id="@+id/button_choose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/choose_button"
android:onClick="onButtonClicked"
android:text="@string/button_choose" />
回答by ρяσ?ρ?я K
you can use setOnTouchListeneras:
您可以将setOnTouchListener用作:
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});