gpt4 book ai didi

android - 如何配置 LongClick 的响应时间?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:14 33 4
gpt4 key购买 nike

在安卓中,View.onLongClickListener()大约需要 1 秒才能将其视为长按。如何配置长按的响应时间?

最佳答案

默认超时由 ViewConfiguration.getLongPressTimeout() 定义.

您可以实现自己的长按:

boolean mHasPerformedLongPress;
Runnable mPendingCheckForLongPress;

@Override
public boolean onTouch(final View v, MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_UP:

if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
if (mPendingCheckForLongPress != null) {
v.removeCallbacks(mPendingCheckForLongPress);
}
// v.performClick();
}

break;
case MotionEvent.ACTION_DOWN:
if( mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new Runnable() {
public void run() {
//do your job
}
};
}


mHasPerformedLongPress = false;
v.postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());

break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();

// Be lenient about moving outside of buttons
int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
if ((x < 0 - slop) || (x >= v.getWidth() + slop) ||
(y < 0 - slop) || (y >= v.getHeight() + slop)) {

if (mPendingCheckForLongPress != null) {
v. removeCallbacks(mPendingCheckForLongPress);
}
}
break;
default:
return false;
}

return false;
}

关于android - 如何配置 LongClick 的响应时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9460170/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com