如何在按钮视图上启用触觉反馈
我想向应用程序的按钮添加触觉反馈,并以编程方式控制它们以显示按钮状态(启用和禁用)。 默认的触觉反馈设置器仅适用于长按。我怎样才能使它适用于简单的按钮点击。
有没有办法对触摸移动等事件进行触觉反馈?
I want to add haptic feedback to my application's buttons and control them programmatically to show button state (enabled and disabled).
The default haptic feedback setter works only for long press. How can i make it work for simple button clicks.
And is there a way to have haptic feedback on events like touch move?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
2019 年 12 月 24 日更新:
必须通过以下方式启用视图功能:
android:hapticFeedbackEnabled="true"
。或者在代码中使用
view.setHapticFeedbackEnabled(true);
(引自 Ivan Chau)
然而,还需要考虑的一件事是在虚拟设备中启用触觉设置。有时这很烦人,所以我们有一些标志来提供帮助(这会以某种方式忽略这些启用设置):
Mayra 的一个例子是,运行触觉反馈是使用此代码。
这行代码可以很容易地包含在您的 onclick 操作中。
这样做的好处是您不需要在 AndroidManifest 中设置权限
(我不需要在 SdkVersion“7” 上使用此功能(2.1 或 2.3 是 7 ))
另请注意,在我的代码中,仅当用户将触觉反馈启用为全局时才会运行。
请参阅http://developer.android.com/reference/android/view/HapticFeedbackConstants。 html 以便始终使用它。
UPDATE ON DECEMBER 24TH 2019:
The view must be enabled Haptic function by:
android:hapticFeedbackEnabled="true"
in xml.Or use
view.setHapticFeedbackEnabled(true);
in code(Cited from Ivan Chau)
However, one more thing to take into consideration is to enable Haptic Setting in virtual devices. This is annoying sometimes, so we have some flags come to help (which will ignore these enable Setting somehow):
An example to Mayra is, for run the Haptic Feedback is by using this code.
And this line of code can easy be include in you onclick action.
The good part with this is you do not need to set a permission in the AndroidManifest
(I do not need this on SdkVersion "7" (2.1 or 2.3 is 7 ))
Also note, in my code here, this will only be running if the user has enabled Haptic Feedback as global.
See http://developer.android.com/reference/android/view/HapticFeedbackConstants.html for alway use it.
这是一个答案,尽管它可能不是最好的实现:
不要忘记,您还需要将“android.permission.VIBRATE”权限添加到程序的清单中。您可以通过将以下内容添加到“AndroidManifest.xml”文件中来实现此目的:
Here is an answer, though it might not be the best implementation:
Don't forget, you also need to add the "android.permission.VIBRATE" permission to the program's manifest. You can do so by adding the following to the "AndroidManifest.xml" file:
View 有一个 PerformHapticFeedback 函数,它应该允许您在任何需要的时候执行它,即在 OnClick 侦听器上。
View has a performHapticFeedback function, which should allow you to perform it whenever you want, i.e., on an OnClick listener.
您可以在活动中使用的简单方法。
a straightforward approach you can use in an activity.
除了前面的答案之外,请确保您的设备设置中启用了“振动反馈”选项
In addition to the previous answers please make sure that "Vibration Feedback" option is enabled from your device settings
就我而言,我试图在对话框片段打开时执行触觉反馈。这个问题的现有答案不起作用。我必须包装
postDelayed
才能使其正常工作。In my case I was trying to preform haptic feedback when a dialog fragment opened. The existing answers to this question weren't working. I had to wrap in
postDelayed
to get it to work.