的onTouchEvent不工作的子视图 [英] OnTouchEvent not working on child views

查看:146
本文介绍了的onTouchEvent不工作的子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线性布局,有一个按钮,并在其上​​一个TextView。我已经写了的onTouchEvent的活动。在code工作正常,如果我触摸屏幕上,但如果我触摸按钮,code不起作用。这究竟是什么可能的解决方案?

 公共布尔的onTouchEvent(MotionEvent事件){
   INT eventaction = event.getAction();


   开关(eventaction)
   {
   案例MotionEvent.ACTION_MOVE:
      reg.setText(哎);


   打破;
   }
   返回true;

  }
 

解决方案

现在的问题是运营为Android如何处理触摸事件的顺序。每个触摸事件遵循的模式(简单的例子):

  1. Activity.dispatchTouchEvent()
  2. ViewGroup.dispatchTouchEvent()
  3. View.dispatchTouchEvent()
  4. View.onTouchEvent()
  5. ViewGroup.onTouchEvent()
  6. Activity.onTouchEvent()

但是事件只能跟随链,直到它们被消耗(意思是有人从的onTouchEvent()或监听器返回true)。在那里,你只需触摸屏幕上的某个地方的情况下,无人问津的情况下,所以它流一路下滑到你的code。然而,在按钮的情况下(或其他可点击查看)它消耗的触摸事件,因为它有兴趣的话,这样的流动停在4号线。

如果你想监视进入你的活动全触控,你需要重写 dispatchTouchEvent()因为什么一直被称为第一,的onTouchEvent()的一个活动被最后调用,且仅当别人捕获的事件。小心不消耗事件在这里,不过,还是孩子的意见永远不会让他们和你的按钮将无法点击。

 公共布尔dispatchTouchEvent(MotionEvent事件){
   INT eventaction = event.getAction();

    开关(eventaction){
      案例MotionEvent.ACTION_MOVE:
          reg.setText(哎);
          打破;
      默认:
          打破;
    }

    返回super.dispatchTouchEvent(事件);
}
 

另一种选择是把你的触摸操控code到自定义的ViewGroup (如的LinearLayout )并使用其 onInterceptTouchEvent()方法,以允许父视图偷掉,并在必要时处理触摸事件。不过要小心,因为这种互动是一个无法挽回,直到一个新的触摸事件开始(一旦你偷一个事件,你偷他们全部)。

心连心

I have a Linear Layout that has a Button and a TextView on it. I have written a OnTouchEvent for the activity. The code works fine if I touch on the screen, but if I touch the button the code does not work. What is the possible solution for this?

public boolean onTouchEvent(MotionEvent event) {
   int eventaction=event.getAction();


   switch(eventaction)
   {
   case MotionEvent.ACTION_MOVE:
      reg.setText("hey");


   break;
   }
   return true;

  }

解决方案

The problem is the order of operations for how Android handles touch events. Each touch event follows the pattern of (simplified example):

  1. Activity.dispatchTouchEvent()
  2. ViewGroup.dispatchTouchEvent()
  3. View.dispatchTouchEvent()
  4. View.onTouchEvent()
  5. ViewGroup.onTouchEvent()
  6. Activity.onTouchEvent()

But events only follow the chain until they are consumed (meaning somebody returns true from onTouchEvent() or a listener). In the case where you just touch somewhere on the screen, nobody is interested in the event, so it flows all the way down to your code. However, in the case of a button (or other clickable View) it consumes the touch event because it is interested in it, so the flow stops at Line 4.

If you want to monitor all touches that go into your Activity, you need to override dispatchTouchEvent() since that what always gets called first, onTouchEvent() for an Activity gets called last, and only if nobody else captured the event. Be careful to not consume events here, though, or the child views will never get them and your buttons won't be clickable.

public boolean dispatchTouchEvent(MotionEvent event) {
   int eventaction=event.getAction();

    switch(eventaction) {
      case MotionEvent.ACTION_MOVE:
          reg.setText("hey");
          break;
      default:
          break;
    }

    return super.dispatchTouchEvent(event);
}

Another option would be to put your touch handling code into a custom ViewGroup (like LinearLayout) and use its onInterceptTouchEvent() method to allow the parent view to steal away and handle touch events when necessary. Be careful though, as this interaction is one that cannot be undone until a new touch event begins (once you steal one event, you steal them all).

HTH

这篇关于的onTouchEvent不工作的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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