Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
We are able to use toasts to alert users when the user enters an empty or data exception in EditText, and we can also use animated and vibrating hints to tell the user that the data you entered is incorrect! Such a way is more friendly and interesting.
In order to complete this requirement, I have encapsulated a helper class that can be easily implemented with this effect.
Let's start with the code.
/* Copyright (c) 2014, Qingdao Science and 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 Shake effect help class * @author Zhaokaiqiang * @date 2014-11-21 9:56:15 * */public class Edittextshakehelper {//Shock animation private Animation shakeanimation;//interpolator private Cycleinterpolator Cycleinter polator;//Vibrator Private Vibrator Shakevibrator;public Edittextshakehelper (Context context) {//Initialize vibrator Shakevibrator = ( Vibrator) Context.getsystemservice (service.vibrator_service);//Initialize motion animation shakeanimation = new Translateanimation (0, 10 , 0, 0); Shakeanimation.setduration (+); cycleinterpolator = new CycleintErpolator (8); Shakeanimation.setinterpolator (cycleinterpolator);} /** * Start Shaking * * @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 small, and in order to use it more convenient, I directly put the animation settings in the code, so that there is no need for additional XML files.
We can call it like this, very easy
if (Textutils.isempty (Et.gettext (). toString ())) {new Edittextshakehelper (mainactivity.this). Shake (ET);}
Do not forget to add the vibration permission before use
< uses-permission android:name="Android.permission.VIBRATE" />
GitHub address for this project: Https://github.com/ZhaoKaiQiang/EditTextShakeHelper
Android Tool class vibration and animation tips for users entering illegal content--edittextshakehelper Tool class introduction