Android 如何在按钮视图上启用触觉反馈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2228151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable haptic feedback on button view
提问by Kshitij Aggarwal
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?
有没有办法对触摸移动等事件进行触觉反馈?
回答by FIG-GHD742
UPDATE ON DECEMBER 24TH 2019:
2019 年 12 月 24 日更新:
The view must be enabled Haptic function by:
视图必须通过以下方式启用触觉功能:
- Add
android:hapticFeedbackEnabled="true"
in xml. Or use
view.setHapticFeedbackEnabled(true);
in code(Cited from Ivan Chau)
android:hapticFeedbackEnabled="true"
在 xml 中添加。或者
view.setHapticFeedbackEnabled(true);
在代码中使用(引用自伊万·周)
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.
Mayra 的一个例子是,通过使用此代码运行触觉反馈。
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 ))
这行代码可以很容易地包含在您的 onclick 操作中。这样做的好处是您不需要在 AndroidManifest 中设置权限(我在 SdkVersion "7" 上不需要这个(2.1 或 2.3 是 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.htmlfor alway use it.
另请注意,在我这里的代码中,只有当用户将触觉反馈启用为全局时,它才会运行。请参阅http://developer.android.com/reference/android/view/HapticFeedbackConstants.html始终使用它。
回答by RyanM
Here is an answer, though it might not be the best implementation:
这是一个答案,尽管它可能不是最好的实现:
import android.view.View;
import android.os.Vibrator;
public class Main extends Activity implements OnClickListener
{
private View myView;
private Vibrator myVib;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
//myView can be any type of view, button, etc.
myView = (View) this.findViewById(R.id.myView);
myView.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
myVib.vibrate(50);
//add whatever you want after this
}
}
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:
不要忘记,您还需要在程序清单中添加“android.permission.VIBRATE”权限。您可以通过将以下内容添加到“AndroidManifest.xml”文件中来实现:
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
I hope that helps.
我希望这有帮助。
回答by Cheryl Simon
View has a performHapticFeedback function, which should allow you to perform it whenever you want, i.e., on an OnClick listener.
View 有一个 performHapticFeedback 函数,它应该允许您随时执行它,即在 OnClick 侦听器上。
回答by Hamid Vakilian
getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
a straightforward approach you can use in an activity.
您可以在活动中使用的直接方法。
回答by Hoby
In addition to the previous answers please make sure that "Vibration Feedback" option is enabled from your device settings
除了之前的答案,请确保从您的设备设置中启用“振动反馈”选项