【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
发布时间:2020-05-24 21:28:47 所属栏目:Java 来源:互联网
导读:【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 转载请注明出处: http://blog.csdn.net/zhaokaiqiang1992 /*
* Copyright (c) 2014,青岛司通科技有限公司 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:输入框震动效果帮助类
* @author zhaokaiqiang
* @date 2014-11-21 上午9:56:15
*
*/
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);
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);
}
}
代码非常的少哈,而且为了使用起来更加方便,我直接把动画的设置写在代码里面了,这样就不需要额外的xml文件了。 if (TextUtils.isEmpty(et.getText().toString())) {
new EditTextShakeHelper(MainActivity.this).shake(et);
}
使用之前不要忘记加上震动的权限 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
