Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

android - How to enable haptic feedback on button view

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE ON DECEMBER 24TH 2019:

The view must be enabled Haptic function by:

  1. Add android:hapticFeedbackEnabled="true" in xml.
  2. 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):

view.performHapticFeedback(
    HapticFeedbackConstants.VIRTUAL_KEY, 
    HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING  // Ignore device's setting. Otherwise, you can use FLAG_IGNORE_VIEW_SETTING to ignore view's setting.
);

An example to Mayra is, for run the Haptic Feedback is by using this code.

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...