转载请注明出处: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. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

  2. MongoDB集群安装与配置2.4.3版本

    mongoDB安装http://www.mongodb.org/downloads拷文件到# cp mongodb-linux-i686-2.4.1.tgz /usr/local/进入目录:# cd ...

  3. c#编程:给定一个正整数求出是几位数并逆序输出

    <span style="color:#FF0000;">第一步:把输入的数字转为字符串n.ToString() 第二步:求出字符串的长度即为正整数的位数 第三步:从后 ...

  4. Setup iOS Development Environment.

    Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...

  5. MySQL误删数据救命指南

    预防误操作导致文件/数据丢失的建议: 1.欲删除文件时,将rm命令改成mv,可在系统层面将rm命令做个alias(或参考Windows / Mac OSX做法,删除文件时先进回收站).2.删除数据库. ...

  6. 在vue中使用babel-polyfill

    在 Vue.js项目中使用Vuex,Vuex 依赖 Promise,所以如果你的浏览器没有实现 Promise (比如 IE),那么就需要使用一个 polyfill 的库 我们可以通过babel-pr ...

  7. ssion机制详解

    ssion机制详解   ref:http://justsee.iteye.com/blog/1570652 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚sess ...

  8. CMakeListx.txt 编辑语法学习

    已hello.cpp为源文件,构建一个CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(hello) add_executable( ...

  9. php实现 求int型数据在内存中存储时1的个数(函数都可自己实现)

    php实现 求int型数据在内存中存储时1的个数(函数都可自己实现) 一.总结 一句话总结:函数我们自己都可以实现,尤其是很多基础函数,没有工具的时候自己写. 1.php进制转换函数? base_co ...

  10. poi读取excell表格

    原文链接:http://blog.csdn.net/qq_37936542/article/details/79024847 最近项目需要实现一个将excell中的数据导入数据库,在网上找到这篇文章, ...