如何在按钮视图上启用触觉反馈

发布于 2024-08-21 08:57:11 字数 114 浏览 6 评论 0原文

我想向应用程序的按钮添加触觉反馈,并以编程方式控制它们以显示按钮状态(启用和禁用)。 默认的触觉反馈设置器仅适用于长按。我怎样才能使它适用于简单的按钮点击。

有没有办法对触摸移动等事件进行触觉反馈?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜尕妞 2024-08-28 08:57:12

2019 年 12 月 24 日更新:

必须通过以下方式启用视图功能:

  1. 在 xml 中添加 android:hapticFeedbackEnabled="true"
  2. 或者在代码中使用view.setHapticFeedbackEnabled(true);

    (引自 Ivan Chau)

然而,还需要考虑的一件事是在虚拟设备中启用触觉设置。有时这很烦人,所以我们有一些标志来提供帮助(这会以某种方式忽略这些启用设置):

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.
);

Mayra 的一个例子是,运行触觉反馈是使用此代码。

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

这行代码可以很容易地包含在您的 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:

  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.

等数载,海棠开 2024-08-28 08:57:12

这是一个答案,尽管它可能不是最好的实现:

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
    }
}

不要忘记,您还需要将“android.permission.VIBRATE”权限添加到程序的清单中。您可以通过将以下内容添加到“AndroidManifest.xml”文件中来实现此目的:

<uses-permission android:name="android.permission.VIBRATE"/>

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:

<uses-permission android:name="android.permission.VIBRATE"/>
蓦然回首 2024-08-28 08:57:12

View 有一个 PerformHapticFeedback 函数,它应该允许您在任何需要的时候执行它,即在 OnClick 侦听器上。

View has a performHapticFeedback function, which should allow you to perform it whenever you want, i.e., on an OnClick listener.

季末如歌 2024-08-28 08:57:12
getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);

您可以在活动中使用的简单方法。

getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);

a straightforward approach you can use in an activity.

兮子 2024-08-28 08:57:12

除了前面的答案之外,请确保您的设备设置中启用了“振动反馈”选项

In addition to the previous answers please make sure that "Vibration Feedback" option is enabled from your device settings

千紇 2024-08-28 08:57:12

就我而言,我试图在对话框片段打开时执行触觉反馈。这个问题的现有答案不起作用。我必须包装 postDelayed 才能使其正常工作。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    preformHapticFeedback()
}

private fun preformHapticFeedback() {
    val delay = 1L
    requireView().postDelayed(
        {
            requireView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
        },
        delay
    )
}

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.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    preformHapticFeedback()
}

private fun preformHapticFeedback() {
    val delay = 1L
    requireView().postDelayed(
        {
            requireView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
        },
        delay
    )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文