"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 can use Toast to alert the user when the user enters an empty or data exception in EditText, and we can also use animation and vibration cues to tell the user that the data you entered is not correct! This way is more friendly and interesting.

To fulfill this requirement, I encapsulated a helper class that can be easily implemented.

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 be more convenient to use, I directly write the animation settings in the code, so that no additional XML files.

We can call it as follows, very simple

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" />

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

Related Article

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.