如何在Android中处理视图外的触摸? [英] How to handle touch outside the view in Android?

查看:13
本文介绍了如何在Android中处理视图外的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 "撤消栏" 用于 Android 的 Gmail 应用程序.UndoBar"基本上是一个显示在布局顶部的视图.

I've found implementation of "Undo Bar" used in Gmail application for Android. "UndoBar" is basically a View that is displayed on top of the layout.

不幸的是,它不完整 - 它没有通过触摸栏外的屏幕来关闭栏的功能.

Unfortunately it's not complete - it has no functionality of dismissing bar by touching screen outside the bar.

我已经实现了 FrameLayout,它覆盖了 onInterceptTouchEvent 来处理 bar 的关闭,但触摸 Action Bar 什么也没做.

I've implemented FrameLayout that overrides onInterceptTouchEvent to handle bar dismissing but touching Action Bar does nothing.

有什么方法可以处理来自操作栏的此类事件?

Is there any way to handle such events from Action Bar?

下面有一个带有UndoBar"的图像.我想要在红点表示的操作栏中处理触摸.

Below there is an Image with "UndoBar"shown. What I want to achieve to handle touch in Action bar represented by red dot.

推荐答案

尝试重写你的activity的dispatchTouchEvent.

Try to override dispatchTouchEvent of your activity.

dispatchTouchEvent(MotionEvent event){

     int x= event.getRawX();
     int y= event.getRawY();


         if(/*check bounds of your view*/){
          // set your views visiblity to gone or what you want. 
         }
      //for prevent consuming the event.
      return super().dispatchTouchEvent(event);    
}

更新

dispatchTouchEvent(MotionEvent event){

     int x= event.getRawX();
     int y= event.getRawY();

      return super().dispatchTouchEvent(event)||[YourView].onTouch(event);    
}

这篇关于如何在Android中处理视图外的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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