//敲重点
eyePassword.setOnTouchListener(this);//为eye图标注册事件监听
@Override
public boolean onTouch(View v, MotionEvent event) {
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.passwordEye){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
eyePassword.setImageResource(R.drawable.open_eye);
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//密码显示
password.setSelection(password.length());//但是密码显示后,文本光标会跑到开头去,重新在文本末获取一下光标
break;
case MotionEvent.ACTION_UP:
eyePassword.setImageResource(R.drawable.close_eye);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码隐藏
password.setSelection(password.length());
break;
}
case MotionEvent.ACTION_DOWN:
eyePassword.setImageResource(R.drawable.open_eye);
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//密码显示
password.setSelection(password.length());//但是密码显示后,文本光标会跑到开头去,重新在文本末获取一下光标
break;
case MotionEvent.ACTION_UP:
eyePassword.setImageResource(R.drawable.close_eye);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码隐藏
password.setSelection(password.length());
break;
}
}
return true;//这里要返回true,不然抬起事件会不响应,应该是事件分发机制的原因
}
}
}
}