【Android工具類】使用者輸入非法內容時的震動與動畫提示——EditTextShakeHelper工具類介紹

來源:互聯網
上載者:User

標籤:

    轉載請註明出處: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工具類介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.