[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Add haptic feedback to events\n\nOne of the most basic use cases for haptics is to provide feedback to user\ninteractions. Time pickers, the key press on a virtual keyboard, and text\nselection are common examples of good use cases for haptic feedback. For more\ninformation about when and how to apply haptics, read\n[Haptics design principles](/develop/ui/views/haptics/haptics-principles).\n\nThis page describes three ways to provide haptic feedback.\n\n- [Use a `View`](#view) **(recommended)** . This approach is action-oriented, has the widest support, and doesn't require the `VIBRATE` permission.\n- [Use a predefined `VibrationEffect`](#predefined_vibrationeffect). This approach has more flexibility, but with some trade-offs.\n- [Use advanced compositions with primitives](#advanced_composition). This method is newer and even more flexible, but requires specific device support.\n\nThese methods use primitives defined at the device level to provide high quality\nfeedback tailored to the device in hand.\n| **Caution:** We strongly discourage using older `Vibrator` methods employing `createOneshot` or `createWaveform`, even if they appear to be supported on the device. These modes are often too loud for regular haptic feedback; use them only as a fallback if you need to highlight an extremely important action.\n\nAll haptic feedback methods respect the user's touch feedback settings by\ndefault.\n\nUse `View` components to generate haptic feedback\n-------------------------------------------------\n\nUse the [`View.performHapticFeedback`](/reference/android/view/View#performHapticFeedback(int)) method to generate haptic feedback. The\nhaptic constants defined by the [`HapticFeedbackConstants`](/reference/android/view/HapticFeedbackConstants) are focused on their\nfunctionality in an application, not the type of haptic effect performed.\n\nThe underlying implementation might vary depending on the device and hardware\ncapabilities, but the app only needs to consider the type of feedback to provide\nin a particular context. By focusing on the functionality, you can enable haptic\nfeedback for similar interactions. Users learn to associate different meanings\nto different haptic sensations over time.\n\n### Prerequisites: Enable haptic feedback\n\nAs long as the [`View`](/reference/android/view/View) is visible, haptic feedback can be used for its events.\nSome events, such as long press, have default haptics that are triggered if a\nlistener on the view handles the event (returns `true`).\n\nAn Android `View` can disable haptic feedback by setting the\n[`View.hapticFeedbackEnabled`](/reference/android/view/View#attr_android:hapticFeedbackEnabled) property to `false`. Disabling this property\nresults in default feedback.\n\nThe [`performHapticFeedback`](/reference/android/view/View#performHapticFeedback(int)) method also honors the system setting\n[`HAPTIC_FEEDBACK_ENABLED`](/reference/android/provider/Settings.System#HAPTIC_FEEDBACK_ENABLED), which allows the user to potentially disable them\nfor the entire system.\n\nUnlike other haptic APIs, using `HapticFeedbackConstants` with a `View`\ndoesn't require the `VIBRATE` permission.\n\n### Choose a `HapticFeedbackConstant`\n\nWhen using `View` components with `HapticFeedbackConstants`, there's no need\nto evaluate specific device support, as these constants will have fallback\nbehavior if necessary. The only consideration is the SDK level of the desired\nconstant.\n\n### Example 1: Keypress\n\nThis is an example of how to add a haptic feedback to a touch input in `View`\nusing touch listeners. The effects simulate the feeling of pressing down on a\nbutton and then releasing it. \n\n### Kotlin\n\n```kotlin\nclass HapticTouchListener : View.OnTouchListener {\n override fun onTouch(View view, MotionEvent event) : Boolean {\n when (event.actionMasked) {\n MotionEvent.ACTION_DOWN -\u003e\n view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)\n MotionEvent.ACTION_UP -\u003e\n view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE)\n }\n return true\n }\n}\n```\n\n### Java\n\n```java\nclass HapticTouchListener implements View.OnTouchListener {\n @Override\n public boolean onTouch(View view, MotionEvent event) {\n switch (event.getAction()) {\n case MotionEvent.ACTION_DOWN:\n view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);\n break;\n case MotionEvent.ACTION_UP:\n view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE);\n break;\n }\n return true;\n }\n}\n```\n\n### Example 2: Submit button\n\nHaptic feedback use cases go beyond simulating a physical interaction with the\ndevice. They might also be used to convey an abstract meaning. For example,\nthe general expectation for a\n[`CONFIRM`](/reference/android/view/HapticFeedbackConstants#CONFIRM) effect is a\nshort and light vibration while a\n[`REJECT`](/reference/android/view/HapticFeedbackConstants#REJECT) might be a\nstronger feedback to signal failure. This is illustrated in the following\nexample for a submit button feedback. \n\n### Kotlin\n\n```kotlin\nsubmitButton.setOnClickListener { view -\u003e\n val successful = performSubmit()\n if (successful) {\n view.performHapticFeedback(HapticFeedbackConstants.CONFIRM)\n } else {\n view.performHapticFeedback(HapticFeedbackConstants.REJECT)\n }\n}\n```\n\n### Java\n\n```java\nsubmitButton.setOnClickListener(view -\u003e {\n boolean successful = performSubmit();\n if (successful) {\n view.performHapticFeedback(HapticFeedbackConstants.CONFIRM);\n } else {\n view.performHapticFeedback(HapticFeedbackConstants.REJECT);\n }\n});\n```\n\nUse a predefined `VibrationEffect` to generate haptic feedback\n--------------------------------------------------------------\n\nUsing the [`View`-based](#view) approach focuses on the user interaction. It is\npreferred for consistency across the system. However, specific predefined\n[`VibrationEffect`](/reference/android/os/VibrationEffect) APIs can also be invoked for customized haptic feedback\neffects.\n\nPredefined effects are available as [`VibrationEffect`\nconstants](/reference/android/os/VibrationEffect#constants_1), and can be\nchecked for support and played with the [`Vibrator`](/reference/android/os/Vibrator) service as shown in the\nfollowing examples.\n\n### Understand device support of `VibrationEffect` APIs\n\nIn basic usage, there should be no need to check for support of individual\n`VibrationEffect` APIs. The APIs such as [`Vibrator.areEffectsSupported`](/reference/android/os/Vibrator#areEffectsSupported(int...))\nand [`Vibrator.areAllEffectsSupported`](/reference/android/os/Vibrator#areAllEffectsSupported(int...)) are used to determine if the device has\na *customized* implementation of the constant. If a customized effect isn't\npresent, your app can still play the effects and use a platform-defined\nfallback implementation.\n\nFor more details, see [Predefined\n`VibrationEffect`](/develop/ui/views/haptics/haptics-apis#predefined_vibration_effect).\n\n### Prerequisites: Load the Vibrator and the `VIBRATE` permission\n\nMost vibrations can be played with the `Vibrator` service, which can be loaded\nas follows: \n\n### Kotlin\n\n```kotlin\nimport android.os.Vibrator\n\nval vibrator = context.getSystemService(Vibrator::class.java)\n```\n\n### Java\n\n```java\nimport android.os.Vibrator;\n\nVibrator vibrator = context.getSystemService(Vibrator.class);\n```\n\nThe app needs to have the\n[`VIBRATE`](/reference/android/Manifest.permission#VIBRATE) permission in order\nto vibrate the device using this service. The permission can be added to the\napplication manifest file: \n\n \u003cuses-permission android:name=\"android.permission.VIBRATE\"/\u003e\n\n### Play a predefined `VibrationEffect`\n\n| **Caution:** Following the [guidelines](/develop/ui/views/haptics/haptics-principles#guidelines), effects should be *clear* and their strengths should correspond with *importance*. If an effect isn't available, consider falling back to nothing rather than a discordant alternative.\n\nPredefined effects can be prepared using [`VibrationEffect.createPredefined`](/reference/android/os/VibrationEffect#createPredefined(int)),\nthen played using one of the `vibrate` methods on [`Vibrator`](/reference/android/os/Vibrator).\n\nThis example plays a Click effect. \n\n### Kotlin\n\n```kotlin\nval vibrator = context.getSystemService(Vibrator::class.java)\n...\n// Requires VIBRATE permission\nvibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK))\n```\n\n### Java\n\n```java\nVibrator vibrator = context.getSystemService(Vibrator.class);\n...\n// Requires VIBRATE permission\nvibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK));\n```\n\nUse advanced compositions with primitives\n-----------------------------------------\n\nThe [`VibrationEffect.Composition`](/reference/android/os/VibrationEffect.Composition) API offers additional possibilities for\nhaptic feedback. However, unlike effects, these primitives don't have\nsystem-level fallbacks, which means that careful attention needs to be paid to\nthe primitives and other capabilities supported by the device.\n\nUsing these APIs is discussed in more detail in\n[*Creating Custom Haptic Effects*](/develop/ui/views/haptics/custom-haptic-effects#vibration_compositions)."]]