[Android tool class] vibration and animation prompts when users enter illegal content-EditTextShakeHelper tool class introduction,

Source: Internet
Author: User

[Android tool class] vibration and animation prompts when users enter illegal content-EditTextShakeHelper tool class introduction,

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

When the user input in EditText is null or the data is abnormal, we can use Toast to remind the user. In addition, we can also use animation effects and vibration prompts to tell the user: the data you entered is incorrect! This method is more friendly and interesting.

To meet this requirement, I encapsulated a help class to easily achieve this effect.

Run the code first.

/** Copyright (c) 2014, Qingdao stone 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 vibration effect help class * @ author zhaokaiqiang * @ date 9:56:15 **/public class EditTextShakeHelper {// vibration Animation private Animation shakeAnimation; // interpolation machine private CycleInterpolator cycleInterpolator; // Vibrator private Vibrator shakeVibrator; public EditTextShakeHelper (Context context) {// initialize the Vibrator shakeVibrator = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); // initialize the animation shakeAnimation = new TranslateAnimation (0, 10, 0, 0); shakeAnimation. setDuration (300); cycleInterpolator = new CycleInterpolator (8); shakeAnimation. setInterpolator (cycleInterpolator);}/*** start to vibrate ** @ 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 rare. In order to make it easier to use, I directly wrote the animation settings in the Code, so that no additional xml file is needed.

We can call it as follows, which is very simple.

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

Do not forget the vibration permission before using it.

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

Github address for this project: https://github.com/ZhaoKaiQiang/EditTextShakeHelper

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.