Android登录实例——计时显示“隐视密码*”

来源:互联网 发布:cad网络综合布线教程 编辑:程序博客网 时间:2024/05/16 15:31

利用倒计时类短暂显视“隐视密码*”

一、前言

        用户体验度是经久不息的话题,学习、继承。

        多数密码框的输入采用密文“*”代为显示,这很大提高了安全度,但有些时候,设置的密码特别的长,如果输错,难以知道是其中哪个位置错了(主要是倒数第几个错了),那就可以只删除错误及其后的地方,接着前面的继续输;或仅仅改正那么一两个错误。完成正确输入。能考虑的尽量考虑,能做到的努怒力去做到,提高用户体验度。

效果如下图,点击/长按小眼2秒,密码由*变为明码(输入的字母或数字):上与下同(上面是“首次输入”,下面是“再次密码确认”)

    

只需要三个步骤:

(1)新建一个”倒计时的内部类“;

(2)新建一个“自定义 长按/长点击 事件”类;

(3)在我们触发窗口调用。


二、内部类继承倒计时类,重写里面的onTick()和onFinish()方法,实现短暂的复现明码密码(显视密码)。

        /*  * 定义一个倒计时的内部类 *  *  @param millisInFuture 总时长 *  @param countDownInterval 时间间隔 *  @param id 使用倒计时的控件id *  */class TimeCount extends CountDownTimer {private int id;public TimeCount(long millisInFuture, long countDownInterval, int id) {super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔// 传入控件的idthis.id = id;}@Overridepublic void onFinish() {// 计时完毕时触发// 密码输入框textView = (TextView) findViewById(id);// 密码隐视textView.setTransformationMethod(PasswordTransformationMethod.getInstance());}@Overridepublic void onTick(long millisUntilFinished) {// 计时过程显示// 密码输入框textView = (TextView) findViewById(id);// 密码可视textView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());}}


三、我们需要在用户长按”显视密码“按钮时,才触发密码的明码短暂显示:因而,需要建立一个”自定义 长按/长点击 事件类“

package com.cn.example.eventhandling;import android.content.Context;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;/** * 自定义 长按/长点击 事件类 *  * @author WQB *  */public class LongPressView extends View {private static final String TAG = "LongPressView";private int mLastMotionX, mLastMotionY;private boolean isMoved;// 长按的runnableprivate Runnable mLongPressRunnable;// 移动的阈值private static final int TOUCH_SLOP = 20;public LongPressView(final Context context) {super(context);mLongPressRunnable = new Runnable() {@Overridepublic void run() {Log.i(TAG, "mLongPressRunnable excuted");performLongClick(); // 执行长按事件(如果有定义的话)}};}/** *  * 1)在down的时候,让一个Runnable在设定时间后执行, 如果设定时间到了,没有移动超过定义的阈值,也没有释放,则触发长按事件 *  * 2)在移动超过阈值和释放之后,会将Runnable从事件队列中remove掉,长按事件也就不会再触发了 */public boolean dispatchTouchEvent(MotionEvent event) {int x = (int) event.getX();int y = (int) event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:mLastMotionX = x;mLastMotionY = y;isMoved = false;/* * 将mLongPressRunnable放进任务队列中,到达设定时间后开始执行 这里的长按时间采用系统标准长按时间 */postDelayed(mLongPressRunnable,ViewConfiguration.getLongPressTimeout());break;case MotionEvent.ACTION_MOVE:if (isMoved)break;if (Math.abs(mLastMotionX - x) > TOUCH_SLOP|| Math.abs(mLastMotionY - y) > TOUCH_SLOP) {// 移动超过阈值,则表示移动了isMoved = true;removeCallbacks(mLongPressRunnable);}break;case MotionEvent.ACTION_UP:// 释放了removeCallbacks(mLongPressRunnable);break;}return true;}}

四、在我们触发窗口调用:(按钮的绑定事件中)

import com.cn.example.eventhandling.LongPressView;public class MainActivity extends ActionBarActivity {// 声明并定义‘倒计时内部类’对象public TimeCount timeCount;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 明码显示密码// 自定义 长按/长点击 事件类View mLongPressViewOne = new LongPressView(this);// 获取显视控件 visualBtn为控件idmLongPressViewOne = (Button) findViewById(R.id.visualBtn);mLongPressViewOne.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {// 传入控件id,调用成员变量,内部类,获取倒计时对象timeCount = new TimeCount(3000, 1000, R.id.passwordText);// 开始倒计时timeCount.start();return false;}});}}

Yes,finish.

0 0
原创粉丝点击