Android随笔-LongClick时长有多长

描述

最近听见有人争论长按的时长有多长,有人说是400毫秒,有人说是500毫秒。其实两种说法都对,AndroidSDK版本不同,时长也不一样。

验证

长按点击事件:

        new View(this).setOnLongClickListener(v -> false);

点击事件执行在MotionEvent.ACTION_DOWN时,也就是手指按下时。长按点击事件其实就是一种延时操作。

                case MotionEvent.ACTION_DOWN:
                    if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
                        mPrivateFlags3 |= PFLAG3_FINGER_DOWN;
                    }
                    mHasPerformedLongPress = false;
                    // 不是单击事件
                    if (!clickable) {
                        // 检查是否是长按事件,若是则执行
                        checkForLongClick(
                                ViewConfiguration.getLongPressTimeout(),
                                x,
                                y,
                                TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS);
                        break;
                    }

其中checkForLongClick:

    private void checkForLongClick(long delay, float x, float y, int classification) {
        if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE || (mViewFlags & TOOLTIP) == TOOLTIP) {
            mHasPerformedLongPress = false;

            if (mPendingCheckForLongPress == null) {
                mPendingCheckForLongPress = new CheckForLongPress();
            }
            mPendingCheckForLongPress.setAnchor(x, y);
            mPendingCheckForLongPress.rememberWindowAttachCount();
            mPendingCheckForLongPress.rememberPressedState();
            mPendingCheckForLongPress.setClassification(classification);
            // post一个延时事件
            postDelayed(mPendingCheckForLongPress, delay);
        }
    }
    ...
    // post延时事件Runnable 
        public boolean postDelayed(Runnable action, long delayMillis) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.postDelayed(action, delayMillis);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().postDelayed(action, delayMillis);
        return true;
    }

其中delay就是长按的延时时间,也就是长按的判断时间,也就是ViewConfiguration.getLongPressTimeout()的时长。


    /**
     * @return the duration in milliseconds before a press turns into
     * a long press
     */
    public static int getLongPressTimeout() {
        return AppGlobals.getIntCoreSetting(Settings.Secure.LONG_PRESS_TIMEOUT,
                DEFAULT_LONG_PRESS_TIMEOUT);
    }

所以长按时长的争议点在于DEFAULT_LONG_PRESS_TIMEOUT的数值。
在AndroidSDK-29中,该时长为500毫秒。


    /**
     * Defines the default duration in milliseconds before a press turns into
     * a long press
     */
    private static final int DEFAULT_LONG_PRESS_TIMEOUT = 500;

在AndroidSDK-28中也是500毫秒。
但是在AndroidSDK-30中,该时长变成了400毫秒。


    /**
     * Defines the default duration in milliseconds before a press turns into
     * a long press
     * @hide
     */
    public static final int DEFAULT_LONG_PRESS_TIMEOUT = 400;

在AndroidSDK-31中为400毫秒。

总结

长按时长在AndroidSDK-29(包含29)及以下版本为500毫秒,大于29版本时长为400毫秒。

要实现自定义Toast显示时间,可以通过以下步骤进行: 1. 创建一个自定义的Toast布局文件,比如命名为custom_toast.xml。 2. 在自定义布局文件中添加一个TextView,并设置其显示内容。 3. 在Java代码中实例化该自定义布局文件,并设置其显示时间。 4. 在事件中触发该自定义Toast显示。 具体的代码实现如下: 1. 创建自定义Toast布局文件custom_toast.xml ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#88000000" android:orientation="horizontal" android:padding="20dp"> <TextView android:id="@+id/custom_toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="18sp" /> </LinearLayout> ``` 2. 在Java代码中实例化该自定义布局文件,并设置其显示时间。 ``` // 实例化自定义布局文件 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, null); // 设置Toast显示的文字内容 TextView text = (TextView) layout.findViewById(R.id.custom_toast_text); text.setText("自定义Toast显示100秒"); // 设置Toast的显示时间为100秒 int toastDurationInMilliSeconds = 100000; Toast toast = new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); ``` 3. 在事件中触发该自定义Toast显示。 ``` button.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 在事件中触发自定义Toast显示 // 实例化自定义布局文件 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, null); // 设置Toast显示的文字内容 TextView text = (TextView) layout.findViewById(R.id.custom_toast_text); text.setText("自定义Toast显示100秒"); // 设置Toast的显示时间为100秒 int toastDurationInMilliSeconds = 100000; Toast toast = new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); return true; } }); ``` 这样就可以在事件中触发一个自定义Toast,并设置其显示时间为100秒了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值