如何启用按钮查看触觉反馈触觉、按钮、反馈

2023-09-12 06:12:47 作者:秋風落葉情

我想触觉反馈添加到我的应用程序的按钮和控制它们编程显示按钮的状态(启用和禁用)。 默认的触觉反馈setter方法​​仅适用于长期preSS。我怎样才能使之成为简单的按钮点击工作。

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?

推荐答案

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

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

不要忘了,你还需要将android.permission.VIBRATE权限添加到程序的清单。您可以通过添加下面的AndroidManifest.xml中文件,这样做的:

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"></uses-permission>

我希望帮助。

I hope that helps.