为什么没有这个MotionEvent模拟工作? [英] Why doesn't this MotionEvent simulation work?
问题描述
在在练习的应用程序的视图中的一个,我试图以编程方式执行文本的选择。
我能够(编程)输入文本选择模式,这是视觉上由CursorControllers(AKA处理)在视图的左上角显示。
如果我的手动拖动右侧CursorController,然后再次单击它(在模拟器),它工作正常(完全),呈现出一条简短的消息:文本复制到剪贴板。 P>
但是,当我试图以编程方式拖动右侧CursorController,什么都不会发生。
我尝试这样做的方式是通过模拟MotionEvent。在视图中,我呼吁:
事件= MotionEvent.obtain(停机,eventTime,MotionEvent.ACTION_DOWN,X,Y,0);
MainActivity.onTouch(这一点,活动);
在课程的MainActivity我实现OnTouchListener:
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){//叫按钮前的的onTouchEvent()
Log.v(MainActivity :: onTouch(),describeEvent(ⅴ,事件));
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
案例MotionEvent.ACTION_UP:
如果(!v.hasFocus()){
v.requestFocus();
}
打破;
}
返回false;
}
如果我理解正确的话,仅仅通过返回假从onTouch,机器人不断寻找另一个UI对象消耗MotionEvent对象,最终到达我的看法
为什么不这样呢?
我一定是失去了一些东西很基本的...
由于缺乏一个有效的解决方案,我只能说我一直在试图完成不可能对Android,出于安全考虑。一种解释可以在以下链接:<一href="http://stackoverflow.com/questions/5240287/how-to-send-synthesized-motionevent-through-the-system/5240821#5240821">How要通过系统发送合成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屋!