Reprinted, please indicate the source:http://blog.csdn.net/zhaokaiqiang1992
When the user enters as empty or data abnormal in Edittext, we can use Toast to remind users. In addition, we can also use the animation effect and vibration prompt to tell the user: the data you entered is wrong. Intersection This method is more friendly and interesting.
In order to fulfill this demand, I encapsulated a help class, which can easily achieve this effect.
first code.
/*
* Copyright (c) 2014, Qingdao Sitong 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.Cleinterpolace;
import android.View.animation.translateanimation;
Import android.widget.edittext;
/**
*
* @Classname: com.example.sharkdemo.edTextShakehelper
* @Descripting: Input box vibration effect help class
* @author zhaokaiqiang
* @date 2014-11-21 9:56:15 AM
*
*/
Public Class EditTextShakehelper {
// Vibration animation
Private Animation Shakeanimation;
// Value
Private CycleInterpolator CycleInterpolace;
// Vibration
Private vibrator shakevibrator;
public EditTextShakehelper (Context Context) {
// Initialized vibrator
shakevibrator = (vibrator) context
.getSystemService (service.vibrator_service);
// Initialized vibration animation
Shakeanimation = New Translateanimation (0, 10, 0, 0);
shakeanimation.setduration (300);
Cycleinterpolator = New CycleInterpootor (8);
shakeanimation.SetInterpootor (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 write the animation settings in the code, so that there is no additional XML file.
We can call like the following, very simple
if (TextUtils.isEmpty(et.getText().toString())) {
new EditTextShakeHelper(MainActivity.this).shake(et);
}
Do not forget to add vibration permissions before using
<uses-permission android:name=“android.permission.VIBRATE” />
GitHub address of this project:https://github.com/ZhaoKaiQiang/EditTextShakeHelper