我想检测一个视图上的doubletap,比如一个按钮,然后知道它是哪个视图.我见过 this similar question,但他们说这是一个重复的问题似乎没有回答我的问题.
我只能find将GestureDetector添加到活动中,并向其添加一个OnDoubleTapListener.但只有在我点击屏幕的背景/布局时才会触发.当我(双击)按钮时不会触发它.
这是我在onCreate中的代码:
gd = new GestureDetector(this,this); gd.setonDoubleTapListener(new OnDoubleTapListener() { @OverrIDe public boolean onDoubleTap(MotionEvent e) { Log.d("OnDoubleTapListener","onDoubleTap"); return false; } @OverrIDe public boolean onDoubleTapEvent(MotionEvent e) { Log.d("OnDoubleTapListener","onDoubleTapEvent"); //if the second tap hadn't been released and it's being moved if(e.getAction() == MotionEvent.ACTION_MOVE) { } else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen { } return false; } @OverrIDe public boolean onSingleTapConfirmed(MotionEvent e) { Log.d("OnDoubleTapListener","onSingleTapConfirmed"); return false; } });解决方法 您只需使用这几行代码即可实现此目的.就这么简单.
final GestureDetector gd = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){ //here is the method for double tap @OverrIDe public boolean onDoubleTap(MotionEvent e) { //your action here for double tap e.g. //Log.d("OnDoubleTapListener","onDoubleTap"); return true; } @OverrIDe public voID onLongPress(MotionEvent e) { super.onLongPress(e); } @OverrIDe public boolean onDoubleTapEvent(MotionEvent e) { return true; } @OverrIDe public boolean onDown(MotionEvent e) { return true; } });//here yourVIEw is the VIEw on which you want to set the double tap actionyourVIEw.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { return gd.ontouchEvent(event); } });
将这段代码放在要在视图上设置双击 *** 作的活动或适配器上.
总结以上是内存溢出为你收集整理的如何在android中的视图上听doubletap?全部内容,希望文章能够帮你解决如何在android中的视图上听doubletap?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)