如何通过onTouchListener区分ACTION_UP和实际点击?
How to distinguish between ACTION_UP and actual click via onTouchListener?
我有一个带有图标的 ImageView
。我希望能够:
1。在悬停时更改图标(意思是 - 在 ACTION_DOWN 上更改为图标 #2,但在 ACTION_UP[ 上也将其更改回图标 #1)
2。触发 onClick 函数(意思是 - 当 ImageView
被 单击 时调用另一个函数)
这就是我的 onTouch
现在的样子:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// CHANGE TO IMAGE #2
break;
case MotionEvent.ACTION_UP:
// CHANGE TO IMAGE #1
break;
}
return true;
}
问题是 - 我不知道用户什么时候按下我的 ImageView
然后离开它的区域 同时仍然按下 。我希望我解释得很好。我的意思是 ACTION_UP 并不总是暗示用户打算 单击 视图。我如何确定 ImageView
何时被 单击 而不仅仅是悬停(并且仍然能够在悬停时更改图标)?
如果使用 选择器 会更好。您可以阅读有关 StateList 的更多信息。网上有很多例子如何使用。
使用Selector Drawable
类似这样的东西。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item android:drawable="@drawable/btn_bg" android:state_focused="false" android:state_pressed="false" />
<!-- button focused -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="true" android:state_pressed="false" />
<!-- button pressed -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="false" android:state_pressed="true" />
</selector>
我有一个带有图标的 ImageView
。我希望能够:
1。在悬停时更改图标(意思是 - 在 ACTION_DOWN 上更改为图标 #2,但在 ACTION_UP[ 上也将其更改回图标 #1)
2。触发 onClick 函数(意思是 - 当 ImageView
被 单击 时调用另一个函数)
这就是我的 onTouch
现在的样子:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// CHANGE TO IMAGE #2
break;
case MotionEvent.ACTION_UP:
// CHANGE TO IMAGE #1
break;
}
return true;
}
问题是 - 我不知道用户什么时候按下我的 ImageView
然后离开它的区域 同时仍然按下 。我希望我解释得很好。我的意思是 ACTION_UP 并不总是暗示用户打算 单击 视图。我如何确定 ImageView
何时被 单击 而不仅仅是悬停(并且仍然能够在悬停时更改图标)?
如果使用 选择器 会更好。您可以阅读有关 StateList 的更多信息。网上有很多例子如何使用。
使用Selector Drawable
类似这样的东西。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item android:drawable="@drawable/btn_bg" android:state_focused="false" android:state_pressed="false" />
<!-- button focused -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="true" android:state_pressed="false" />
<!-- button pressed -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="false" android:state_pressed="true" />
</selector>