3

使用 jetpack compose,对于 clickevent 如何执行触觉反馈。我是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()))
        }
    }
}

当文本被选中时,我能够看到文本,但没有得到微妙的振动反馈。

4

4 回答 4

5

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

val haptic = LocalHapticFeedback.current
val context = LocalContext.current
Row(
    Modifier.clickable {
        haptic.performHapticFeedback(HapticFeedbackType.LongPress)
    }
)
于 2021-07-11T13:18:14.657 回答
1

目前仅(撰写 UI 1.1.0-beta03LongPressTextHandleMove通过以下方式支持

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很多常量

于 2021-12-09T06:25:52.630 回答
0

Modifier.clickable {...}在应用程序中添加反馈逻辑到Row

于 2021-07-11T06:31:44.140 回答
0

在我的手机(以及我测试过的其他设备)上,手机既不振动LongPress也不TextHandleMove振动。

在我们像这样迁移到 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 访问它,就像Daniele Segato 的回答一样:

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

于 2022-01-25T18:22:21.553 回答