[Android tool class] vibration and animation prompts when users enter illegal content-EditTextShakeHelper tool class introduction,
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
When the user input in EditText is null or the data is abnormal, we can use Toast to remind the user. In addition, we can also use animation effects and vibration prompts to tell the user: the data you entered is incorrect! This method is more friendly and interesting.
To meet this requirement, I encapsulated a help class to easily achieve this effect.
Run the code first.
/** Copyright (c) 2014, Qingdao stone Technology Co., Ltd. All rights reserved. * File Name: EditTextShakeHelper. java * Version: V1.0 * Author: zhaokaiqiang * Date: 2014-11-21 */package com. example. sharkdemo; import android. app. service; import android. content. context; import android. OS. vibrator; import android. view. animation. animation; import android. view. animation. cycleInterpolator; import android. view. animation. translateAnimation; import android. widget. editText;/***** @ ClassName: com. example. sharkdemo. editTextShakeHelper * @ Description: input box vibration effect help class * @ author zhaokaiqiang * @ date 9:56:15 **/public class EditTextShakeHelper {// vibration Animation private Animation shakeAnimation; // interpolation machine private CycleInterpolator cycleInterpolator; // Vibrator private Vibrator shakeVibrator; public EditTextShakeHelper (Context context) {// initialize the Vibrator shakeVibrator = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); // initialize the animation shakeAnimation = new TranslateAnimation (0, 10, 0, 0); shakeAnimation. setDuration (300); cycleInterpolator = new CycleInterpolator (8); shakeAnimation. setInterpolator (cycleInterpolator);}/*** start to vibrate ** @ param editTexts */public void shake (EditText... editTexts) {for (EditText editText: editTexts) {editText. startAnimation (shakeAnimation);} shakeVibrator. vibrate (new long [] {0,500},-1 );}}
The code is very rare. In order to make it easier to use, I directly wrote the animation settings in the Code, so that no additional xml file is needed.
We can call it as follows, which is very simple.
if (TextUtils.isEmpty(et.getText().toString())) {new EditTextShakeHelper(MainActivity.this).shake(et);}
Do not forget the vibration permission before using it.
<Uses-permission android: name = "android. permission. VIBRATE"/>
Github address for this project: https://github.com/ZhaoKaiQiang/EditTextShakeHelper