安卓:自定义浏览onClickEvent与X'放大器; Y坐标 [英] Android: custom view onClickEvent with X & Y locations

查看:195
本文介绍了安卓:自定义浏览onClickEvent与X'放大器; Y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图,我想要得到的X和用户点击Y坐标。我发现,我只能得到从的onTouchEvent和onClickEvent坐标将不会闪光,如果我有一个的onTouchEvent。不幸的是,当用户拖动屏幕和点击。在onTouchEventfires

I've got a custom view and I want to get the X and Y coordinates of a user click. I've found that I can only get the coordinates from the onTouchEvent and an onClickEvent won't fire if I have an onTouchEvent. Unfortunately the onTouchEventfires when the user drags the screen as well as clicking.

我试图在两者之间区别,但是我还是当我拖着code射击:

I've tried to differentiate between the two, but I still get the code firing when I'm dragging:

 public boolean onTouchEvent(MotionEvent event) {
  int action = event.getAction();
  if (action == MotionEvent.ACTION_DOWN) {
                //fires on drag and click

我看了一下但正如我上面提到的我不'T认为解决方案将有工作,我不能让onClick的,并在同一时间工作的onTouch事件。也许我做错了什么在这方面,有没有处理的自定义事件捕捉用户输入一个正常的方式?我应该能够在同一时间使用的onClick和onTouch事件?

I've had a look at this but as I mentioned above I don't think the solution there will work as I can't get the onClick and the onTouch events working at the same time. Maybe I'm doing something wrong in this respect, is there a normal way of dealing with capturing user input on custom events? Should I be able to use the onClick and onTouch events at the same time?

谢谢,马丁

推荐答案

现在,我不知道的onTouchEvent短路的onClick,我不知道,也许你需要设置机器人:可点击为真(或 setClickable(真))。但是,如果你不能使它工作干净,你可以使用一个布尔在的onTouchEvent模仿点击检测,如果一切都失败了。在ACTION_DOWN,将其设置为true(isClicking或类似的东西),在ACTION_MOVE将其设置为false(因为用户因此拖着不去点击),然后在ACTION_UP可以测试它。如果这是真的,这意味着用户pressed下来,但不能拖动。这是哈克,但如果你不能得到的onClick工作,它基本上是同样的事情。

Now, I don't know about onTouchEvent short circuiting onClick, and I don't know if maybe you need to set android:clickable to true (or setClickable(true)). But, if you can't make it work cleanly, you can use a boolean in the onTouchEvent to mimic click detection if all else fails. in ACTION_DOWN, set it to true (isClicking or something like that), in ACTION_MOVE set it to false (because the user is dragging therefore not clicking) and then in ACTION_UP you can test for it. If it's true, this means the user has pressed down but not dragged. It's hacky, but if you can't get onClick to work it's basically the same thing.

这篇关于安卓:自定义浏览onClickEvent与X'放大器; Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆