整个屏幕的 onTouchListener [英] onTouchListener for entire screen

查看:24
本文介绍了整个屏幕的 onTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满按钮的屏幕,但希望 onTouch 方法使用整个屏幕的坐标.我首先尝试使用带有 onTouchListener 的 RelativeLayout,但从未设法使其与侦听器连接"(即触摸屏幕时什么也没发生),我还尝试将 ImageView 放在屏幕顶部,然后使该视图不可见.

I have a screen filled with buttons, but want the onTouch-method to use the entire screen's coordinates. I first tried using a RelativeLayout with an onTouchListener, but never managed to make it "connect" with the listener (i.e. nothing happened when screen was touched), I also tried putting an ImageView on top of the screen, and then making this view invisible.

最后一个方法对 onClicks 给出了正确的响应,但我从来没有设法让它不可见.

This last method gave correct responses to onClicks, but I never managed to make it invisible.

如果这是我非常怀疑的最佳解决方案,我如何使 ImageView 完全不可见,同时又不丢失它的 onTouchListener(我已经尝试过使用白色 backgroundColor 和 setAlpha(0)).

If this is the best solution, which I highly doubt, how do I make the ImageView totally invisible, without losing its onTouchListener (I've experimented with white backgroundColor and setAlpha(0)).

我可以使用全局坐标使 onTouchListener 对整个屏幕做出反应,同时屏幕显示(和改变)几个按钮(最好没有不可见的图像视图)?

Can I somehow make the onTouchListener react to the whole screen, using global coordinates, while the screen is showing (and altering) several buttons (preferably without the invisible imageview)?

如果您不明白我的要求,请随时投诉.我会根据需要尝试填补空白.

If you don't understand what I'm asking for, feel free to complain about that. I'll try to fill the gaps as needed.

我现在已经通过使用常规的 onTouch 方法解决了这个问题.我在使 ACTION_DOWN 和 ACTION_MOVE 激活按钮时遇到了几个问题,但我终于让它工作了.对于阅读本文的其他人:可能会使用 onInterceptTouchEvent(但我从未想出如何获取屏幕坐标而不是视图坐标).

I've now managed to resolve the issue by using the regular onTouch-method. I ran into several problems making both ACTION_DOWN and ACTION_MOVE activate the buttons, but I finally got it working. For other people reading this: onInterceptTouchEvent could possibly be used (but I never figured out how to get the screen coordinates instead of the view-coordinates).

推荐答案

对不起,如果我错了,但我相信我刚刚遇到了类似的问题.我想要一个显示图片的标题屏幕,并且在图片上写着点击继续"或类似的字样.我折腾了一下,发现你可以让布局变得可点击.

Sorry if I'm wrong, but I believe I've just had a similar problem. I wanted a title screen that displayed a picture and on the picture words that say "Click to go on" or something similar. I messed around for a bit and found that you can make a layout clickable.

    android:focusable="true" 
    android:id="@+id/titlescreenframe">

在我的布局的 xml 文件中.背景图片只是在背景属性中(我意识到你没有使用图片)

is in my xml file for my layout. The background image is simply in the background attribute (I realize you aren't using images)

无论如何,回到我的活动中

Anyway, back in my activity

    private FrameLayout fl;
    ...
    fl = (FrameLayout)findViewById(R.id.titlescreenframe);
    fl.setOnClickListener(this);

然后我使用 switch 语句来处理那个和下一个布局上的按钮.如果需要,请点击此处:使用 Switch 语句处理按钮点击

And then I use a switch statement to handle that and the buttons that are on the next layout. Here if you need it: Using Switch Statement to Handle Button Clicks

似乎这也适用于其他布局,而且我的主布局实际上没有任何视图.(除非布局本身算作一个?)

Seems this should work with other layouts as well, and I don't have literally any views on my main layout. (unless the layout itself counts as one?)

哈!刚刚意识到你说你找到了解决方案.愚蠢的时机.我会在偶然的情况下发布这对某人有帮助,祝大家编码愉快.:)

Ha! Just realized you said you found the solution. Silly timing. I'll post on the off-chance this helps someone, happy coding everyone. :)

这篇关于整个屏幕的 onTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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