Get the App
SLTechnology News&Howtos  ›  Development  › 

How to click the button to vibrate in Android application

Shulou Source: shulou.com Published: 2022-06-03 09:40:26 10月01日 Update

This article will explain in detail how to click button vibrations in Android applications. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The details are as follows

In the project, you need to click the button in the application to have a vibrating effect. The vibration effect can be turned off in the settings.

The specific implementation is as follows.

Write vibration help class: VibrateHelp.java

Add the following permissions to the import android.content.Context;import android.os.Vibrator; / * vibration help class * androidManifest.xml * * / public class VibrateHelp {private static Vibrator vibrator / * simple vibration * @ param context calls the Context * @ param millisecond of the vibration, millisecond * / @ SuppressWarnings ("static-access") public static void vSimple (Context context, int millisecond) {vibrator = (Vibrator) context.getSystemService (context.VIBRATOR_SERVICE); vibrator.vibrate (millisecond) } / * * complex vibrations * @ param context calls the Context * @ param pattern vibration form of vibrations * @ number of param repeate vibrations,-1 does not repeat, non-1 repeats * / @ SuppressWarnings ("static-access") public static void vComplicated (Context context, long [] pattern, int repeate) {vibrator = (Vibrator) context.getSystemService (context.VIBRATOR_SERVICE); vibrator.vibrate (pattern, repeate) } / * stop shaking * / public static void stop () {if (vibrator! = null) {vibrator.cancel ();}

The following custom implementation button clicks the vibration class: ViewClickVibrate.java

Import android.view.View;import android.view.View.OnClickListener; public class ViewClickVibrate implements OnClickListener {/ * * Button Vibration time * / private final int VIBRATE_TIME = 60; @ Override public void onClick (View v) {/ / TODO determines whether to vibrate VibrateHelp.vSimple (v.getContext (), VIBRATE_TIME) according to the flag in the setting;}}

Finally, when listening to the button, use the class ViewClickVibrate, as follows: MainActivity.java

Import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button; public class MainActivity extends Activity {Button btn1; Button btn2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); btn1 = (Button) findViewById (R.id.button1); btn2 = (Button) findViewById (R.id.button2) Btn1.setOnClickListener (new ViewClickVibrate () {public void onClick (View v) {super.onClick (v); / / TODO}}); btn2.setOnClickListener (new ViewClickVibrate () {public void onClick (View v) {super.onClick (v); / / TODO}});} @ Override public boolean onCreateOptionsMenu (Menu menu) {/ / Inflate the menu; this adds items to the action bar if it is present. GetMenuInflater () .inflate (R.menu.main, menu); return true;}}

You can also customize the click events of other controls to achieve the click vibration effect, similar to the above change.

This is the end of the article on "how to click button shock in Android applications". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Vibrations buttons applications effects articles help content time more good complex practical subscript events forms controls articles permissions tags number of times Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Microsoft Shulou Information NVidia