In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.