为什么在打开光标时不总是触发OnTouchEvent? [英] Why OnTouchEvent is not always fired when cursor is on?

查看:78
本文介绍了为什么在打开光标时不总是触发OnTouchEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我为开发中的应用程序使用OnLongClickListenersetOnClickListener之前.有时它会带来一些响应性问题.我使用事件开头的Log.d消息(logcat)检查了它.

基于 Stackoverflow 的一些报告,我决定仅使用onTouchListener事件来模拟所有内容.

这是我的真实代码.

var otl  = OnTouchListener { v, event ->
  val actual = SystemClock.elapsedRealtime()
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x;  editY = event.y
      downClick = atual  // Down touch time 
      vPrev = v.id       // widget in down touch 
      Log.d("myMess", "Down $atual milis")
    } // down
    MotionEvent.ACTION_UP -> {   

      val tag = v.tag as CalcTag      // Se below comments
      val localCheck = tag.name.name == "DISPLAY"  
      var ok =(v.id==vPrev)    // same widget?
          // "Fake" Double click, I'm using just in one situation
      eDoubleClick = (actual - upClick) < doubleClickTime
      upClick = atual   // up touch time
      if (localCheck && ok)  // same 
         ok =  abs(event.x - editX) <=20.0   // "Near" position
               &&  abs(event.x - editX) <=20.0
      if (ok) {    // same widget and position?
        Log.d("myMess", "Up $actual milis: ${event.x} e ${event.y}")
        if ((actual - downClick) > longClickTime)
          clickL(v)  // long click
        else
          clickS(v)  // single click (double click inside!)
      } else
        Log.d("myMess", 
         "Wrong Up $actual milis: ${event.x} e ${event.y}")
     } // up
 //  else-> Log.d("myMess", "Event ${event.action}")
  } // when
  false
} // On Touch

在这里,我为所有小部件设置了触摸事件.这只是一个例行程序.

...
bt.setOnTouchListener(otl)  // bt is related to all widgets 

变量editXeditY(保存位置),downClickupClick(保存时间)和vPrev(按下事件中的先前小部件ID)是全局变量.

关于上述在val tag = v.tag as CalcTag处的注释,CalcTag是存储在小部件tag字段中的结构,其中名称字段存储具有小部件类型的enum.

在这种情况下,DISPLAY是唯一的大尺寸的窗口小部件类型(实际上,只有1个窗口小部件具有这种类型),并且触摸的位置是相关的,因为这是光标所在的位置. /p>

我注意到了什么?

当窗口小部件DISPLAY中的光标关闭(isCursorVisible=false)时,与我的旧OnLongClickListenersetOnClickListener事件一样,我的挥动式单击也起作用.

而且,就像以前一样,有时当我打开光标(isCursorVisible=false)时,特别是当我快速单击时,该小部件不会触发事件触摸.

总结一下,有以下几点:

a)我的程序其余部分对该讨论都没有关系,因为问题都是自包含的.
b)我与窗口小部件关联的唯一事件是触摸事件.
c)我在此处编写的所有内容都是在触摸事件开始时从logcat得出的,没有进行调试,当然还有我的应用程序中的视觉效果,即触摸时缺乏响应能力未检测到.
d)DOWNUP之外发生的唯一事件是2(MOVE),这与我的问题无关.它们在提起手指之前指的是手指的微动.
e)游标的打开和关闭似乎是解决我的问题的关键,因为Android系统会自动更新游标的位置.
f)我已经禁用了与光标相关的所有功能,但自己的光标除外 (禁用EditText上下文菜单)

我该怎么办?

是否需要将光标替换为软件光标,以便在光标下方加下划线?有什么我没得到的吗?

我正在寻求帮助.我感到绝望!

解决方案

令人难以置信.我累了.之前,我已经返回了false,因为touch事件已准备好click事件.但是,它不再是那样了.触摸事件已结束.因此,只需更改true的最后一行即可完全解决问题.

完整的代码变为:

var otl  = OnTouchListener { v, event ->
  val actual = SystemClock.elapsedRealtime()
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x;  editY = event.y
      downClick = atual  // Down touch time 
      vPrev = v.id       // widget in down touch 
      Log.d("myMess", "Down $atual milis")
    } // down
    MotionEvent.ACTION_UP -> {   

      val tag = v.tag as CalcTag      // Se below comments
      val localCheck = tag.name.name == "DISPLAY"  
      var ok =(v.id==vPrev)    // same widget?
          // "Fake" Double click, I'm using just in one situation
      eDoubleClick = (actual - upClick) < doubleClickTime
      upClick = atual   // up touch time
      if (localCheck && ok)  // same 
         ok =  abs(event.x - editX) <=20.0   // "Near" position
               &&  abs(event.x - editX) <=20.0
      if (ok) {    // same widget and position?
        Log.d("myMess", "Up $actual milis: ${event.x} e ${event.y}")
        if ((actual - downClick) > longClickTime)
          clickL(v)  // long click
        else
          clickS(v)  // single click (double click inside!)
      } else
        Log.d("myMess", 
         "Wrong Up $actual milis: ${event.x} e ${event.y}")
     } // up
 //  else-> Log.d("myMess", "Event ${event.action}")
  } // when
  true  // That's all, folks
} // On Touch

这个故事的奇怪之处在于它与false完美配合,仅当其中一个小部件的光标打开时才间歇运行.我发现目前还不清楚该错误是如何表现出来的.他应该锁定一切,什么也不做,并且几乎永远不工作!

Before I've used OnLongClickListener and setOnClickListener for my developing app. Sometimes it was giving some responsiveness problems. I checked it using Log.d messages (logcat) in the head of the events.

Based on some reports from Stackoverflow, I've decided to simulate everything just with the onTouchListener event.

This is my real code.

var otl  = OnTouchListener { v, event ->
  val actual = SystemClock.elapsedRealtime()
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x;  editY = event.y
      downClick = atual  // Down touch time 
      vPrev = v.id       // widget in down touch 
      Log.d("myMess", "Down $atual milis")
    } // down
    MotionEvent.ACTION_UP -> {   

      val tag = v.tag as CalcTag      // Se below comments
      val localCheck = tag.name.name == "DISPLAY"  
      var ok =(v.id==vPrev)    // same widget?
          // "Fake" Double click, I'm using just in one situation
      eDoubleClick = (actual - upClick) < doubleClickTime
      upClick = atual   // up touch time
      if (localCheck && ok)  // same 
         ok =  abs(event.x - editX) <=20.0   // "Near" position
               &&  abs(event.x - editX) <=20.0
      if (ok) {    // same widget and position?
        Log.d("myMess", "Up $actual milis: ${event.x} e ${event.y}")
        if ((actual - downClick) > longClickTime)
          clickL(v)  // long click
        else
          clickS(v)  // single click (double click inside!)
      } else
        Log.d("myMess", 
         "Wrong Up $actual milis: ${event.x} e ${event.y}")
     } // up
 //  else-> Log.d("myMess", "Event ${event.action}")
  } // when
  false
} // On Touch

Here I set the touch event for all my widget. It is just one routine.

...
bt.setOnTouchListener(otl)  // bt is related to all widgets 

The variableseditX, editY (saved positions), downClick, upClick (saved times) e vPrev (previous widget id in down touch event) are global variables.

Concerning about above comment at val tag = v.tag as CalcTag , CalcTag is a structure stored in the widget tag field, where the name field stores an enum with the widget type.

In this case, DISPLAY is the only widget type (In fact, only 1 widget has this type) that has large size and the position of the touch is relevant, because this is where the cursor will be positioned.

What I've noticed?

When cursor in the widget DISPLAY is turned off (isCursorVisible=false), my widgted clicks works in the same way that it worked with my old OnLongClickListener and setOnClickListener events.

And also, like before, when I turn on the cursor (isCursorVisible=false) sometimes, specially when I click fast, the widget don't fires the event touch.

To summarize there are the following points:

a) The rest of my program no matters for this discussion, because the problem is all self-contained.
b) The only event I'm associating with my widgets is the touch event.
c) Everything I wrote here I concluded from the logcat in the head of the touch event, without debugging, and of course by the visual result in my app, which is the lack of responsiveness when touch is not detected.
d) The only event outside DOWN and UP that happens on touch is 2 (MOVE), which is irrelevant to my problem. They refer to the micro movements of the finger before lifting it.
e) Cursor on and off seems to be a key to my problem, because Android system update automatically the cursor position.
f) I've disabled all features related to cursor, except the own cursor (Disable EditText context menu)

What can I do?

Need I replace the cursor for a sofware cursor, like to underline the character under the cursor? Is there something that I don't get?

I'm begging for help. I'm in despair!

解决方案

It's unbelievable. I feel tired. Before, I've returned false because touch event prepared the click event. However, it's not anymore like that. The touch event is the end of the trail. So just to change the last line for true has solved the problem fully.

The complete code becomes:

var otl  = OnTouchListener { v, event ->
  val actual = SystemClock.elapsedRealtime()
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x;  editY = event.y
      downClick = atual  // Down touch time 
      vPrev = v.id       // widget in down touch 
      Log.d("myMess", "Down $atual milis")
    } // down
    MotionEvent.ACTION_UP -> {   

      val tag = v.tag as CalcTag      // Se below comments
      val localCheck = tag.name.name == "DISPLAY"  
      var ok =(v.id==vPrev)    // same widget?
          // "Fake" Double click, I'm using just in one situation
      eDoubleClick = (actual - upClick) < doubleClickTime
      upClick = atual   // up touch time
      if (localCheck && ok)  // same 
         ok =  abs(event.x - editX) <=20.0   // "Near" position
               &&  abs(event.x - editX) <=20.0
      if (ok) {    // same widget and position?
        Log.d("myMess", "Up $actual milis: ${event.x} e ${event.y}")
        if ((actual - downClick) > longClickTime)
          clickL(v)  // long click
        else
          clickS(v)  // single click (double click inside!)
      } else
        Log.d("myMess", 
         "Wrong Up $actual milis: ${event.x} e ${event.y}")
     } // up
 //  else-> Log.d("myMess", "Event ${event.action}")
  } // when
  true  // That's all, folks
} // On Touch

The strange thing about this story is that it worked perfectly with false, it was only intermitent when the cursor of one of the widgets was on. I find it very unclear how the error manifests itself. He should lock everything and do nothing and not work almost always!

这篇关于为什么在打开光标时不总是触发OnTouchEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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