您的位置:首页 > 移动开发 > Android开发

用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍

2016-10-14 16:57 525 查看
        当用户在EditText中输入为空或者是数据异常的时候,我们可以使用Toast来提醒用户,除此之外,我们还可以使用动画效果和震动提示,来告诉用户:你输入的数据不对啊!这种方式更加的友好和有趣。
    为了完成这个需求,我封装了一个帮助类,可以很方便的实现这个效果。

    先上代码吧。

/*
* File Name:EditTextShakeHelper.java
*/
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;

public class EditTextShakeHelper {

// 震动动画
private Animation shakeAnimation;
// 插值器
private CycleInterpolator cycleInterpolator;
// 振动器
private Vibrator shakeVibrator;

public EditTextShakeHelper(Context context) {

// 初始化振动器
shakeVibrator = (Vibrator) context
.getSystemService(Service.VIBRATOR_SERVICE);
// 初始化震动动画
shakeAnimation = new TranslateAnimation(0, 10, 0, 0);
shakeAnimation.setDuration(300);
cycleInterpolator = new CycleInterpolator(8);
shakeAnimation.setInterpolator(cycleInterpolator);

}

/**
* 开始震动
*
* @param editTexts
*/
public void shake(EditText... editTexts) {
for (EditText editText : editTexts) {
editText.startAnimation(shakeAnimation);
}

shakeVibrator.vibrate(new long[] { 0, 500 }, -1);

}

}

我们可以像下面这样调用,非常简单
if (TextUtils.isEmpty(et.getText().toString())) {
new EditTextShakeHelper(MainActivity.this).shake(et);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android EditText震动
相关文章推荐