使用编程设定在Android触摸输入 [英] Use programming to set touch input on android
问题描述
我想使用编程以模拟在画面上的位置(x,y)的一个接触。
I want to use programming to mimic a "touch" on a position (x, y) on the screen.
我看到这个的链接,但它只是教你如何注入的字符。
I saw this link, but it only teaches you how to inject characters.
我想触摸的位置注入(X,Y)
I want to inject a touch at position (x, y)
我如何做到这一点使用的Android SDK(这是用java)?
How do I do this using Android SDK (which is java)?
推荐答案
可以假触摸事件是这样的:
You can "fake" a touch event like this:
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
view.onTouchEvent(event);
API文档:
创建一个新的MotionEvent,填补了 基本运动值的子集。 那些在这里没有规定是:设备 ID(始终为0),pressure和大小 (始终为1),x和y precision(总是 1),和edgeFlags(始终为0)。
Create a new MotionEvent, filling in a subset of the basic motion values. Those not specified here are: device id (always 0), pressure and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
参数:
-
停机
的时间(毫秒)时, 用户最初$ pssed下来,打开P $ 位置事件流。本 必须从获得 SystemClock.uptimeMillis()。 -
eventTime
的时间(毫秒)时, 生成此特定事件。 这必须从以下地址获得SystemClock.uptimeMillis()
。 -
动作
的动作的类型是 完成 - 一个无论是ACTION_DOWN
,ACTION_MOVE
,ACTION_UP
或ACTION_CANCEL
。 -
X
的X此事件的坐标。 -
是
的y本次活动的协调。 -
亚状态
任何元的状态/ 修改键是为有效 当生成事件。
downTime
The time (in ms) when the user originally pressed down to start a stream of position events. This must be obtained from SystemClock.uptimeMillis().eventTime
The the time (in ms) when this specific event was generated. This must be obtained fromSystemClock.uptimeMillis()
.action
The kind of action being performed -- one of eitherACTION_DOWN
,ACTION_MOVE
,ACTION_UP
, orACTION_CANCEL
.x
The X coordinate of this event.y
The Y coordinate of this event.metaState
The state of any meta / modifier keys that were in effect when the event was generated.
<一个href="http://developer.android.com/reference/android/view/MotionEvent.html#obtain(long,%20long,%20int,%20float,%20float,%20int)"相对=nofollow>链接到API文档
这篇关于使用编程设定在Android触摸输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!