java - 即使用户不再触摸按钮,是否可以延长 android 上的按钮触摸效果?
问题描述
我需要能够让应用程序运行起来,就好像按钮触摸的持续时间比现实世界中实际的时间长。
例如,用户触摸一个按钮 1 秒,应用程序在该触摸上添加了 X 量的预定义时间,设备认为用户在 X 量时间过去后才停止按下按钮,即使用户已经移动他们的手指在 X 时间设置之前设置,这恰好在用户停止触摸按钮的那一刻设置。
解决方案
在您的视图中添加触摸侦听器并在 onTouch() 方法中实现您的逻辑。像这样:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getEventTime() > Expected time) {
// implement your logic here
AlertDialog alertDialog = new
AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
button.setEnabled(false);
return true;
}
return true;
}
});
推荐阅读
- python - 更改 QInputDialog 的 CancelButtonText
- spring - Hibernate-JPA 中的无限递归
- java - 两次读取 InputStream 的一部分
- c# - 如何让一个类负责构建另一个类
- javascript - 如果输入的数字是 4 位或 7 位,如何通过 JavaScript 中的 reg 表达式检查,然后相应地执行函数 hexToRGB?
- pycharm - 如何在 PyCharm IDE 中折叠几行选定的代码?
- wpf - WPF网格列全宽
- python - SQL - 重新加载页面后的新查询
- python - 需要一些关于如何解决以下代码的规范?
- javascript - Javascript插入对象以形成数据