MotionEvent没有得到构造函数,我想在单元测试中手动创建一个MotionEvent,然后如何获得它?谢谢.
rek*_*eru 92
您应该使用该类的静态obtain
方法之一MotionEvent
来创建新事件.
最简单的方法(除了从现有事件中包装新事件)是:
static public MotionEvent obtain(long downTime, long eventTime, int action,
float x, float y, int metaState) {
Run Code Online (Sandbox Code Playgroud)
API文档:
创建一个新的MotionEvent,填充基本运动值的子集.这里未指定的是:设备ID(始终为0),压力和大小(始终为1),x和y精度(始终为1)和edgeFlags(始终为0).
参数:
downTime
用户最初按下以开始位置事件流的时间(以毫秒为单位).这必须从SystemClock.uptimeMillis()获得.eventTime
生成此特定事件的时间(以毫秒为单位).这必须从中获得
SystemClock.uptimeMillis()
.action
正在执行的动作的类型-中的任何一个
ACTION_DOWN
,ACTION_MOVE
,
ACTION_UP
,或ACTION_CANCEL
.x
此事件的X坐标.y
此事件的Y坐标.metaState
生成事件时生效的任何元/修饰键的状态.Sur*_*gch 11
以下是一个说明已接受答案的示例:
// get the coordinates of the view
int[] coordinates = new int[2];
myView.getLocationOnScreen(coordinates);
// MotionEvent parameters
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
int action = MotionEvent.ACTION_DOWN;
int x = coordinates[0];
int y = coordinates[1];
int metaState = 0;
// dispatch the event
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
myView.dispatchTouchEvent(event);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
45710 次 |
最近记录: |