如何在 Jetpack Compose 中执行触觉反馈

How to perform a haptic feedback in Jetpack Compose

使用jetpack compose,对于点击事件如何执行触觉反馈。我是 Jetpack Compose 的新手。 这是我试过的-

val hapticFeedback = LocalHapticFeedback

@Composable
fun Tab() {
    Row() {
        Icon(imageVector = icon, contentDescription = text)
        if (selected) {
            // i tried both the following ways, none are working. 
            hapticFeedback.current.performHapticFeedback(
                HapticFeedbackType(10)
            )
            hapticFeedback.current.performHapticFeedback(HapticFeedbackType.TextHandleMove)
....
            Spacer(Modifier.width(12.dp))
            Text(text.uppercase(Locale.getDefault()))
        }
    }
}

我可以在选中文本时看到它,但没有收到细微的振动反馈。

Modifier.clickable {...} 中添加反馈逻辑应用于 Row

在版本 rc-01 的 Compose 中,您只能使用两种类型的触觉反馈:HapticFeedbackType.LongPressHapticFeedbackType.TextHandleMove

val haptic = LocalHapticFeedback.current
val context = LocalContext.current
Row(
    Modifier.clickable {
        haptic.performHapticFeedback(HapticFeedbackType.LongPress)
    }
)

目前(Compose UI 1.1.0-beta03)只有 LongPressTextHandleMove 通过

支持
val haptic = LocalHapticFeedback.current
Button(onClick = {
    haptic.performHapticFeedback(HapticFeedbackType.LongPress)
}) { ... }

如@nglauber 回答所说。

我猜是因为 Compose Desktop 的多平台支持。

还有另一种方法,但是,如果您使用 Android 并且不需要 Compose Desktop 兼容性,则使用它相当容易:

val view = LocalView.current
Button(onClick = {
    view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
}) { ... }

HapticFeedbackConstants class 有 a lot of constants.

在我的 phone(以及我测试过的其他设备)上,LongPressTextHandleMove 都不会使 phone 振动。

在我们像这样移动到 Compose 之前,我们解决了这个问题:

import android.content.Context
import android.view.HapticFeedbackConstants
import android.view.View
import android.view.accessibility.AccessibilityManager

fun View.vibrate() = reallyPerformHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
fun View.vibrateStrong() = reallyPerformHapticFeedback(HapticFeedbackConstants.LONG_PRESS)

private fun View.reallyPerformHapticFeedback(feedbackConstant: Int) {
    if (context.isTouchExplorationEnabled()) {
        // Don't mess with a blind person's vibrations
        return
    }
    // Either this needs to be set to true, or android:hapticFeedbackEnabled="true" needs to be set in XML
    isHapticFeedbackEnabled = true

    // Most of the constants are off by default: for example, clicking on a button doesn't cause the phone to vibrate anymore
    // if we still want to access this vibration, we'll have to ignore the global settings on that.
    performHapticFeedback(feedbackConstant, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING)
}

private fun Context.isTouchExplorationEnabled(): Boolean {
    // can be null during unit tests
    val accessibilityManager = getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager?
    return accessibilityManager?.isTouchExplorationEnabled ?: false
}

现在我们仍然必须使用此代码并像 中那样从 Compose 访问它:

@Composable
fun VibratingButton() {
    val view = LocalView.current
    Button(onClick = {
        view.vibrate()
    }) { ... }
}