想起来了onTouch事件(Android版) [英] Getting JUST the onTouch event (Android)

查看:145
本文介绍了想起来了onTouch事件(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这里分开的几件事情。

I am trying to separate a few things in here.

我有Imagebuttons的程序。他们必须重视他们onTouchListeners。

I have a program with Imagebuttons. They have onTouchListeners attached to them.

我想触摸事件,只是带着淡淡的被解雇,没有与点击。我的意思是,如果我用鼠标点击,例如,我不希望连接到的ImageButton的onTouch事件被解雇。然而,当你点击鼠标移到按钮,它被激发。

I wish touch event to be fired JUST with a touch, not with a click. I mean, if I use a mouse to click, for example, I don't want the onTouch event attached to the ImageButton to be fired. However it IS fired when you click the mouse over the button.

是否有可能只是当触摸发生触发事件?

Is it possible to fire the event JUST when a touch happens?

我的code:

myImageButton.setOnTouchListener(new Button.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
                 if (arg1.getAction() == android.view.MotionEvent.ACTION_DOWN) {    
                    Toast.makeText(LiVoiceActivity.this,
                                    "You touched me!",
                                    Toast.LENGTH_LONG).show();
                }
                 return true;
            }
    });

感谢您!

推荐答案

还有被誉为MotionEvent类Tool_Type的字段。我已经实现了鼠标类型,这里检查:

There's a field known as a Tool_Type in the MotionEvent class. I have implemented a check for the mouse type here:

API 14 AKA EASY MODE

myImageButton.setOnTouchListener(new Button.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent arg1) {
                     if (arg1.getAction() == android.view.MotionEvent.ACTION_DOWN
                              && (MotionEvent.TOOL_TYPE_MOUSE != arg1.getToolType(0)) {    
                        Toast.makeText(LiVoiceActivity.this,
                                        "You touched me!",
                                        Toast.LENGTH_LONG).show();
                    }
                     return true;
                }
        });

API 9

myImageButton.setOnTouchListener(new Button.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent arg1) {
                     if (arg1.getAction() == android.view.MotionEvent.ACTION_DOWN
                              && (arg1.getSize() > 1) {    
                        Toast.makeText(LiVoiceActivity.this,
                                        "You touched me!",
                                        Toast.LENGTH_LONG).show();
                    }
                     return true;
                }
        });

现在这个检查MotionEvent的大小好评。 preSUMABLY,点击鼠标将有一个大小为1,因此,只有认清周围的尺寸大于1.播放与该号码,看看你是否可以将鼠标和手指触摸进行区分。

Now this checks the size of the MotionEvent received. PRESUMABLY, a mouse click would have a size of 1, therefore, only recognize sizes bigger than 1. Play around with that number and see if you can differentiate between the mouse and finger touch.

这篇关于想起来了onTouch事件(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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