为什么MotionEvent模拟不工作? [英] Why doesn't this MotionEvent simulation work?
问题描述
我可以(以编程方式)输入文本选择模式,视图左上角的CursorControllers(AKA手柄)可视地指示。
如果我手动拖动右侧的CursorController,然后再次点击它(在模拟器中),它按预期(完美)工作,显示一条简短的消息:文本复制到剪贴板。
但是当我尝试以编程方式拖动正确的CursorController,没有任何反应。
我尝试这样做的方法是模拟MotionEvent。在视图中,我打电话给:
event = MotionEvent.obtain(downTime,eventTime,MotionEvent.ACTION_DOWN,x,y,0 );
MainActivity.onTouch(this,event);
在MainActivity我当然实现OnTouchListener:
$ {code> @Override
public boolean onTouch(View v,MotionEvent event){//调用BEFORE按钮的onTouchEvent()
Log.v(MainActivity :: onTouch (),describeEvent(v,event));
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if(!v.hasFocus()){
v .requestFocus();
}
break;
}
返回false;
}
如果我理解正确,只需从onTouch返回false, Android不断寻找另一个UI对象来使用MotionEvent对象,最终达到我的观点。
为什么不会发生?
我必须遗漏一些非常基本的东西...
解决方案,我只能得出结论,为安全起见,我一直在努力完成的事情在Android上是不可能的。以下链接可以找到解释:如何通过系统发送合成的MotionEvent?
In one of the views in an exercise app, am trying to perform text selection programmatically.
I am able to (programmatically) enter "text selection mode", which is visually indicated by CursorControllers (AKA handles) on the top-left corner of the view.
If I manually drag the right CursorController, then click it again (in the emulator), it works as expected (perfectly), showing a brief message: "Text copied to clipboard".
But when I try to programmatically drag that right CursorController, nothing happens.
The way I try to do this is by simulating a MotionEvent. In the view, I call:
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
MainActivity.onTouch(this, event);
In the MainActivity I of course implement OnTouchListener:
@Override
public boolean onTouch(View v, MotionEvent event) { // called BEFORE button's onTouchEvent()
Log.v("MainActivity::onTouch()", describeEvent(v, event));
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
If I understand correctly, by mere returning 'false' from onTouch, Android keeps looking for another UI object to consume the MotionEvent object, eventually reaching my view.
Why doesn't this happen?
I must be missing something very fundamental...
For lack of a working solution, I can only conclude that what I have been trying to accomplish isn't possible on the Android, for reasons of security. An explanation can be found in the following link: How to send synthesized MotionEvent through the system?
这篇关于为什么MotionEvent模拟不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!