Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
We can use Toast to alert the user when the user enters an empty or data exception in EditText, and we can also use animation and vibration cues to tell the user that the data you entered is not correct! This way is more friendly and interesting.
To fulfill this requirement, I encapsulated a helper class that can be easily implemented.
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 be more convenient to use, I directly write the animation settings in the code, so that no additional XML files.
We can call it as follows, very simple
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" />
"Android Tool class" vibration and animation tips for users entering illegal content--edittextshakehelper Tool class Introduction