"Android Tool class" vibration and animation tips for users entering illegal content--edittextshakehelper Tool class Introduction

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

We are able to use toasts to alert users when the user enters an empty or data exception in EditText, and we can also use animated and vibrating hints to tell the user that the data you entered is incorrect! Such a way is more friendly and interesting.

In order to complete this requirement, I have encapsulated a helper class that can be easily implemented with this effect.

Let's start with the code.

/* Copyright (c) 2014, Qingdao Science and Technology Co., Ltd. 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: Input box Shake effect help class * @author Zhaokaiqiang * @date 2014-11-21 9:56:15 * */public class Edittextshakehelper {//Shock animation private Animation shakeanimation;//interpolator private Cycleinterpolator Cycleinter polator;//Vibrator Private Vibrator Shakevibrator;public Edittextshakehelper (Context context) {//Initialize vibrator Shakevibrator = ( Vibrator) Context.getsystemservice (service.vibrator_service);//Initialize motion animation shakeanimation = new Translateanimation (0, 10 , 0, 0); Shakeanimation.setduration (+); cycleinterpolator = new CycleintErpolator (8); Shakeanimation.setinterpolator (cycleinterpolator);} /** * Start Shaking * * @param edittexts */public void Shake (EditText ... edittexts) {for (EditText edittext:edittexts) {EditText . Startanimation (shakeanimation);} Shakevibrator.vibrate (new long[] {0, 500},-1);}}

The code is very small, and in order to use it more convenient, I directly put the animation settings in the code, so that there is no need for additional XML files.

We can call it like this, very easy

if (Textutils.isempty (Et.gettext (). toString ())) {new Edittextshakehelper (mainactivity.this). Shake (ET);}

Do not forget to add the vibration permission before use

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

GitHub address for this project: Https://github.com/ZhaoKaiQiang/EditTextShakeHelper

Android Tool class vibration and animation tips for users entering illegal content--edittextshakehelper Tool class introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.