转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992

当用户在EditText中输入为空或者是数据异常的时候,我们能够使用Toast来提醒用户,除此之外,我们还能够使用动画效果和震动提示,来告诉用户:你输入的数据不正确啊!这样的方式更加的友好和有趣。

为了完毕这个需求,我封装了一个帮助类,能够很方便的实现这个效果。

先上代码吧。

/*
* 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, 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文件了。

我们能够像以下这样调用,很easy

if (TextUtils.isEmpty(et.getText().toString())) {
new EditTextShakeHelper(MainActivity.this).shake(et);
}

使用之前不要忘记加上震动的权限

<uses-permission android:name="android.permission.VIBRATE" />

这个项目的github地址:https://github.com/ZhaoKaiQiang/EditTextShakeHelper

【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍的更多相关文章

  1. jquery+php实现用户输入搜索内容时自动提示

    index.html <html> <head>     <meta charset=;} #search_auto li a:hover{background:#D8D ...

  2. AutoCompleteTextView 与sqlite绑定实现记住用户输入的内容并自动提示

    把用户输入的内容保存到数据库表中,然后用户输入时,进行模糊查询并把查询结果附到AutoCompleteTextView中. 1:activity_main.xml <LinearLayout x ...

  3. js打印保存用户输入的内容

    在用js打印局部页面时,遇到用户新输入的内容没能打印出来,经过观察,发现我采用的js打印方法是读取页面源代码,而用户输入的内容如果不将其写入到页面源代码中去,是打印不出来的,下面是我的解决方法: // ...

  4. Android 实现动态匹配输入的内容 AutoCompleteTextView和MultiAutoCompleteTextView

    AutoCompleteTextView1.功能:动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容显示匹配的热门信息.2.独特属性:android:completionThreshold 设 ...

  5. 大量无线键盘存在KeySniffer漏洞-可嗅探用户输入的内容

    几美元的一根天线.一个无线发射器,还有几行Python代码,有了这些,黑客就可以在几十米开外主动地记录下你的用户名.密码.信用卡.你写的稿子,总之就是你用无线键盘输入的任何东西. 黑客所利用的是一种无 ...

  6. 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights

    敏感词文件内容: 代码: def filtered_words(path='filtered_words.txt'): words = [] with open(path, 'r', encoding ...

  7. (3)润写一个程序(类),用户输入一段英文,然后输出这段英文中所有长度为3个字母的单词井且如果单词如果有连续 复了2次,只输出一个【例: This isis a desk,程序输出 his is a desk】,(提示,有re正则匹配来做)

    import re x = input('Please input a string:') pattern = re.compile(r'\b[a-zA-Z]{3}\b') print(pattern ...

  8. tornado当用户输入的URL无效时转入设定的页面

    今天做web的测验..坑爹的要用tornado...作为一个比较新的用的人还不多的东东...查资料好麻烦.. 下面是当用户输入非法 url时, 显示一个自定义 404 页面提示用户,其访问的页面不存在 ...

  9. 防御XSS攻击-encode用户输入内容的重要性

    一.开场先科普下XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶 ...

随机推荐

  1. Android5.0(Lollipop) BLE蓝牙4.0+浅析code(二)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23347612来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  2. Eclipse查看某个方法被哪些类调用

    方法一:打开该类,在类的定义上即类名上,右键-->References--->Project ,就可以查看该类是否被工程中的其他Java文件引用过:但是如果在JSP页面,这个方法查不出来 ...

  3. Vivado 2017.2 SDK 生成FSBL时存在的bug

    SDK 2017.1/.2 - ld.exe: cannot find -lrsa When importing a new HDF file into the SDK or after a clea ...

  4. Socket编程模型之完毕port模型

    转载请注明来源:viewmode=contents">http://blog.csdn.net/caoshiying?viewmode=contents 一.回想重叠IO模型 用完毕例 ...

  5. [Yarn] Use Yarn to Create an Alternative Import Name of an Installed Library

    In this lesson we'll show how to use yarn to alias the names of same npm libraries but install diffe ...

  6. GAN(Generative Adversarial Networks) 初步

    1. Generator vs. Discriminator 首先需要指出的是生成式模型(generative models)和判别式模型(discriminative models)的区别: dis ...

  7. Tor (洋葱头)torbrowser

    Tor是什么 Tor是互联网上用于保护您隐私最有力的工具之一,但是时至今日仍有许多人往往认为Tor是一个终端加密工具.事实上,Tor是用来匿名浏览网页和邮件发送(并非是邮件内容加密)的.今天,我们要讨 ...

  8. Opencv Sift和Surf特征实现图像无缝拼接生成全景图像

    Sift和Surf算法实现两幅图像拼接的过程是一样的,主要分为4大部分: 1. 特征点提取和描述 2. 特征点配对,找到两幅图像中匹配点的位置 3. 通过配对点,生成变换矩阵,并对图像1应用变换矩阵生 ...

  9. 使用Redis做产品统计的两种模式

    http://zihua.li/2012/07/two-patterns-of-statistics-using-redis/ 产品运行过程中及时记录收集并分析统计数据对产品的持续改进有重要的指导作用 ...

  10. erlang抽象码与basho的protobuf

    erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...