android如何判断点击事件次数,android中根据touch事件判断单击及双击

private static final int MAX_INTERVAL_FOR_CLICK = 250;

private static final int MAX_DISTANCE_FOR_CLICK = 100;

private static final int MAX_DOUBLE_CLICK_INTERVAL = 500;

int mDownX = 0;

int mDownY = 0;

int mTempX = 0;

int mTempY = 0;

boolean mIsWaitUpEvent = false;

boolean mIsWaitDoubleClick = false;

Runnable mTimerForUpEvent = new Runnable() {

public void run() {

if (mIsWaitUpEvent) {

Log.d(LOG_TAG,

"The mTimerForUpEvent has executed, so set the mIsWaitUpEvent as false");

mIsWaitUpEvent = false;

} else {

Log.d(LOG_TAG,

"The mTimerForUpEvent has executed, mIsWaitUpEvent is false,so do nothing");

}

}

};

@Override

public boolean onTouchEvent(MotionEvent event) {

if (!mIsWaitUpEvent && event.getAction() != MotionEvent.ACTION_DOWN) {

return super.onTouchEvent(event);

}

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

mDownX = (int) event.getX();

mDownY = (int) event.getY();

mIsWaitUpEvent = true;

postDelayed(mTimerForUpEvent, MAX_INTERVAL_FOR_CLICK);

break;

case MotionEvent.ACTION_MOVE:

mTempX = (int) event.getX();

mTempY = (int) event.getY();

if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK

|| Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {

mIsWaitUpEvent = false;

removeCallbacks(mTimerForUpEvent);

Log.d(LOG_TAG, "The move distance too far:cancel the click");

}

break;

case MotionEvent.ACTION_UP:

mTempX = (int) event.getX();

mTempY = (int) event.getY();

if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK

|| Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {

mIsWaitUpEvent = false;

removeCallbacks(mTimerForUpEvent);

Log.d(LOG_TAG,

"The touch down and up distance too far:cancel the click");

break;

} else {

mIsWaitUpEvent = false;

removeCallbacks(mTimerForUpEvent);

onSingleClick();

return super.onTouchEvent(event);

}

case MotionEvent.ACTION_CANCEL:

mIsWaitUpEvent = false;

removeCallbacks(mTimerForUpEvent);

Log.d(LOG_TAG, "The touch cancel state:cancel the click");

break;

default:

Log.d(LOG_TAG, "irrelevant MotionEvent state:" + event.getAction());

}

return super.onTouchEvent(event);

}

Runnable mTimerForSecondClick = new Runnable() {

@Override

public void run() {

if (mIsWaitDoubleClick) {

Log.d(LOG_TAG,

"The mTimerForSecondClick has executed,so as a singleClick");

mIsWaitDoubleClick = false;

// at here can do something for singleClick!!

} else {

Log.d(LOG_TAG,

"The mTimerForSecondClick has executed, the doubleclick has executed ,so do thing");

}

}

};

public void onSingleClick() {

if (mIsWaitDoubleClick) {

onDoubleClick();

mIsWaitDoubleClick = false;

removeCallbacks(mTimerForSecondClick);

} else {

mIsWaitDoubleClick = true;

postDelayed(mTimerForSecondClick, MAX_DOUBLE_CLICK_INTERVAL);

}

}

public void onDoubleClick() {

Log.d(LOG_TAG,"we can do sth for double click here");

}

原文:http://blog.csdn.net/mydreamongo/article/details/38405659

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是模拟 Android 系统双击事件的脚本的代码示例: ``` import time from android.permission import request_permissions, Permission request_permissions([Permission.TOUCHSCREEN]) from android import Android droid = Android() # 获取屏幕的宽度和高度 screen_width, screen_height = droid.getScreenSize() # 计算屏幕的心点 screen_center_x = screen_width / 2 screen_center_y = screen_height / 2 # 双击屏幕心点 droid.touch(screen_center_x, screen_center_y, 'DOWN_AND_UP') time.sleep(0.1) droid.touch(screen_center_x, screen_center_y, 'DOWN_AND_UP') ``` 这段代码使用了 `android` 库来模拟触摸屏幕的操作。首先,使用 `request_permissions` 函数请求触摸屏幕的权限。然后,使用 `Android` 类创建一个 Android 对象,通过调用它的 `getScreenSize` 方法获取屏幕的宽度和高度,并计算出屏幕的心点。最后,通过调用 Android 对象的 `touch` 方法模拟双击屏幕心点的操作。 希望这些内容能够帮到你。 ### 回答2: 要模拟Android系统的双击事件,可以使用Python的monkeyrunner工具来实现。以下是一个简单的模拟双击事件的脚本示例: ```python from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # 连接设备 device = MonkeyRunner.waitForConnection() # 设置点击坐标和延迟时间 x = 500 y = 500 delay = 0.5 # 第一次点击 device.touch(x, y, MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(delay) # 第二次点击 device.touch(x, y, MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(delay) # 脚本执行完成,断开设备连接 device.dispose() ``` 上述脚本首先通过`MonkeyRunner.waitForConnection()`连接到设备,然后通过`device.touch()`方法模拟触摸事件。在这个示例,我们设置了点击的坐标为`(500, 500)`,延迟时间为0.5秒,即两次点击之间的间隔。 可以根据实际需求修改点击坐标和延迟时间。执行脚本后,即可在设备上模拟双击事件。 需要注意的是,使用这个脚本需要安装并配置好Android SDK和monkeyrunner工具,并确保设备已连接到计算机上。 ### 回答3: 当模拟Android系统双击事件的脚本,你可以使用MonkeyRunner工具。MonkeyRunner是Android SDK的一个命令行工具,可以用于自动化测试和模拟用户交互。 首先,你需要安装好Android SDK,并配置好环境变量。 然后,你可以创建一个.py的Python脚本,编写以下代码来实现双击事件模拟: ```python from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # 连接设备 device = MonkeyRunner.waitForConnection() # 设置点击坐标 x = 500 y = 500 # 模拟点击事件 device.touch(x, y, MonkeyDevice.DOWN_AND_UP) # 单击 MonkeyRunner.sleep(0.5) # 等待一段时间 device.touch(x, y, MonkeyDevice.DOWN_AND_UP) # 再次单击 ``` 在以上代码,我们首先使用`MonkeyRunner.waitForConnection()`方法来连接设备。 然后,我们设置了一个点击坐标`(x, y)`。你可以根据需要调整这个坐标,以确保点击在你想要的位置。 接着,我们使用`device.touch()`方法模拟了双击事件。我们首先调用一次`MonkeyDevice.DOWN_AND_UP`来模拟点击动作,然后等待一段时间(例如0.5秒),再次调用一次`MonkeyDevice.DOWN_AND_UP`来模拟第二次点击动作。 最后,你可以在命令行运行这个脚本:`monkeyrunner your_script.py`,其`your_script.py`是你保存以上代码的脚本文件。 这样的脚本可以用于自动化测试或者模拟特定的用户交互行为,来测试Android应用程序的响应性或功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值