我需要能够使应用程序发挥作用,就好像按钮触摸的持续时间比现实世界中实际持续的时间更长。
例如,用户触摸按钮 1 秒,应用程序会在该触摸上添加 X 段预定义时间,设备认为用户在 X 段时间过去后刚刚停止按下按钮,即使用户在 X 时间设置之前移动了手指,这恰好设置在用户停止触摸按钮的时刻。
最佳答案
在 View 中添加触摸监听器并在 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;
}
});
关于java - 即使用户不再触摸按钮,是否可以延长 Android 上的按钮触摸效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56247748/