android - 如何检测android中按钮的按下时间?
问题描述
setOnTouchListener
我想通过使用withonTouch
方法检测按钮的按下时间
我怎么能做到这一点?
解决方案
也许是这样的:
long mLastClickTime;
findViewById(R.id.button).setOnTouchListener(new OnTouchListener() {
@Override
public void onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
mLastClickTime = SystemClock.elapsedRealtime();
else if (event.getAction() == MotionEvent.ACTION_UP) {
long duration = SystemClock.elapsedRealtime() - mLastClickTime;
// using threshold of 1000 ms
if (duration > 1000) {
// do your magic here
}
}
}
});
推荐阅读
- automation - 解析一组 cisco 交换机,编译 IP 和接口列表,然后将 netmiko 脚本指向该新列表。可能的?
- javascript - Zurb Foundation:作为 ES 模块导入时,滑块插件不会触发事件
- c++ - ld 无法使用 g++ 命令找到 /lib64/libmvec.so.1
- events - 如何为全日历时间网格视图(天/周)包含显示更多按钮?
- javascript - Nuxt 项目中的 Firebase Analytics 错误“未定义导航器”
- ffmpeg - FFmpeg:无法为“mpegts”找到合适的输出格式
- json - 防止空值从 JSON 响应 Flutter 中显示在 ListView 中
- python - 在python中运行for循环以获得更多的迭代次数
- c - 在没有任何格式说明符的情况下将字符串文字作为参数传递给打印函数?
- azure - 从 Graph API 获取所有已登录/登录的 AD B2C 用户