用户输入非法内容时的震动与动画提示——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);
}
为了完成这个需求,我封装了一个帮助类,可以很方便的实现这个效果。
先上代码吧。
/*
* 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工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
- 用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
- 【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
- 【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
- 【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
- 在文本框中提示用户输入内容格式的方法
- 在文本框中提示用户应该输入内容格式的方法
- AutoCompleteTextView 与sqlite绑定实现记住用户输入的内容并自动提示
- jquery+php实现用户输入搜索内容时自动提示
- jquery+php实现用户输入搜索内容时自动提示
- 在文本框中提示用户输入内容格式的方法
- jquery+php实现用户输入搜索内容时自动提示
- .NET+JS对用户输入内容进行字数提示功能的实例代码
- canvas基础动画-循环从屏幕下方出现-上方消失(可切换速度,字号,单色,彩色,显示用户输入内容)
- excel提示:输入值非法,其他用户已经限定了可以输入该单元格的数值
- .NET+JS对用户输入内容进行字数提示功能的实例代码
- jquery+php实现用户输入搜索内容时自动提示
- 对用户输入内容进行字数提示功能
- window.prompt提示框点击取消后返回的是"",点击确定后返回的是用户输入内容。