我应使用哪些 Android 事件来完成此任务?
我有一个与鼠标侦听器相关的 Java 类,我想将其转换到我的 Android 应用程序,但无法找到必要的事件。
我的 Java 应用程序使用以下方法:
- mouseClicked
- mousePressed
- mouseReleased
我想做类似的事情,但不是使用单击事件,而是使用触摸事件。我遇到了 OnTouchListener
并重写了 onTouch
方法。
mousePressed
和 mouseReleased
的替代方案是什么?
编辑 - (在 Peter 回复后更新)
以下事件是否正确:
- ACTION_DOWN : mouseClicked
- ACTION_MOVE : mousePressed
- ACTION_UP : mouseReleased
编辑 - 2 示例来源
我的活动目前没有任何 OnTouchListener
因为我希望可以将所有触摸逻辑保留在我的视图中。
视图:
/*Inside my View - Is it proper to do onTouch logic here?
Or should I be doing this from the Activity?*/
public class myView {
public boolean onTouch(MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//draw arrow when screen is simply touched
break;
case MotionEvent.ACTION_MOVE:
//Do Logic
break;
case MotionEvent.ACTION_UP:
//Do Logic
break;
}
}
}
我在视图中执行逻辑的原因是因为我想直接获取一些变量,而不是创建多个额外的 get 方法。
我可以这样做吗?或者我是否必须重写 Activity 中的 onTouch 方法并在那里执行逻辑?
I have a Java class that pertains to mouse listeners that I'm wanting to convert over to my Android app but can't quite find the necessary events.
My Java app makes use of the following methods:
- mouseClicked
- mousePressed
- mouseReleased
I'm wanting to do something similar however not with click events but touch events. I have come across OnTouchListener
and did an override on the onTouch
method.
What are the alternatives to mousePressed
and mouseReleased
?
Edit - (updated after Peter's response)
Are the following events correct:
- ACTION_DOWN : mouseClicked
- ACTION_MOVE : mousePressed
- ACTION_UP : mouseReleased
EDIT - 2 Example Source
My Activity doesn't have any OnTouchListener
at the moment because I was hoping I could keep all the touch logic in my View.
View:
/*Inside my View - Is it proper to do onTouch logic here?
Or should I be doing this from the Activity?*/
public class myView {
public boolean onTouch(MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//draw arrow when screen is simply touched
break;
case MotionEvent.ACTION_MOVE:
//Do Logic
break;
case MotionEvent.ACTION_UP:
//Do Logic
break;
}
}
}
The reason I am doing the logic in my View is because I have some variables that I would like to grab directly rather than creating multiple extra get methods.
Am I able to do it like this? Or will I have to override the onTouch method in my Activity and do the logic there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有触摸事件(向下、向上、移动、多点触控)均通过“onTouch”处理。请参阅本教程:
All touch events (down, up, move, multitouch) are handled via 'onTouch'. See this tutorial: http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775
如果您想注册
View
上的点击,请实现并添加 OnClickListener 到它。当你想注册触摸事件时,你需要实现 OnTouchListener并将其添加到该
View
中。If you want to register clicks on a
View
, implement and add an OnClickListener to it.When you want to register touch events you need to implement the OnTouchListener and add it to that
View
.