android – 在编辑文本中可绘制,在错误后不更新

前端之家收集整理的这篇文章主要介绍了android – 在编辑文本中可绘制,在错误后不更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
android中编辑文本不允许在setError之后更改drawable.
我在密码字段中使用了drawable right,但如果密码字段中出现错误,则不允许在其后更改drawable.错误之前它工作正常.
  1. <EditText
  2. android:id="@+id/edt_reg_password"
  3. style="@style/editText_full_view"
  4. android:layout_height="wrap_content"
  5. android:layout_below="@id/edt_reg_email"
  6. android:layout_marginTop="@dimen/padding_normal"
  7. android:drawableLeft="@mipmap/ic_action_password"
  8. android:drawableRight="@mipmap/ic_action_password_visibility"
  9. android:drawablePadding="@dimen/padding_normal"
  10. android:hint="@string/hint_password"
  11. android:inputType="textPassword"
  12. android:maxLength="25"
  13. android:paddingLeft="@dimen/padding_normal"
  14. tools:visibility="visible" />

用于更改眼睛图标运行时的Java代码

  1. private void setPasswordDrawable()
  2. {
  3. final Drawable showpass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility);
  4. final Drawable hidepass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility_off);
  5.  
  6.  
  7. final Drawable pass_drawable = getResources().getDrawable(R.mipmap.ic_action_password);
  8. pass_drawable.setBounds(0,pass_drawable.getIntrinsicWidth(),pass_drawable.getIntrinsicHeight());
  9.  
  10.  
  11. //edtPassword.setCompoundDrawables(pass_drawable,null,showpass_icon,null);
  12.  
  13. edtPassword.setOnTouchListener(new View.OnTouchListener() {
  14. @Override
  15. public boolean onTouch(View v,MotionEvent event) {
  16. if (edtPassword.getCompoundDrawables()[2] == null) {
  17. return false;
  18. }
  19. if (event.getAction() != MotionEvent.ACTION_UP) {
  20. return false;
  21. }
  22. if (event.getX() > edtPassword.getWidth() - edtPassword.getPaddingRight()
  23. - showpass_icon.getIntrinsicWidth()) {
  24.  
  25. if (isPasswordVisible) {
  26.  
  27. runOnUiThread(new Runnable() {
  28. @Override
  29. public void run() {
  30.  
  31. //edtPassword.setError(null);
  32. edtPassword.setTransformationMethod(
  33. PasswordTransformationMethod.getInstance());
  34. edtPassword.setSelection(edtPassword.getText().length());
  35.  
  36. showpass_icon.setBounds(0,showpass_icon.getIntrinsicWidth(),showpass_icon.getIntrinsicHeight());
  37. edtPassword.setCompoundDrawables(pass_drawable,null);
  38.  
  39.  
  40. }
  41. });
  42.  
  43. isPasswordVisible = false;
  44. } else {
  45.  
  46. runOnUiThread(new Runnable() {
  47. @Override
  48. public void run() {
  49.  
  50. //edtPassword.setError(null);
  51. edtPassword.setTransformationMethod(
  52. HideReturnsTransformationMethod.getInstance());
  53. edtPassword.setSelection(edtPassword.getText().length());
  54. hidepass_icon.setBounds(0,hidepass_icon.getIntrinsicWidth(),hidepass_icon.getIntrinsicHeight());
  55. edtPassword.setCompoundDrawables(pass_drawable,hidepass_icon,null);
  56. }
  57. });
  58.  
  59. isPasswordVisible = true;
  60. }
  61. }
  62. return false;
  63. }
  64. });
  65.  
  66. }

用于设置错误

  1. public void setViewError(View view,String message)
  2. {
  3. if (view instanceof EditText) {
  4. ((EditText) view).setError(message);
  5. }
  6. }

解决方法

你可以用这个 –
  1. if(error=true){
  2. editText.setCompoundDrawablesWithIntrinsicBounds(
  3. 0,R.drawable.ic_error,0);
  4. editText.setCompoundDrawablePadding(5);}
  5. else{
  6. editText.setCompoundDrawablesWithIntrinsicBounds(
  7. 0,R.drawable.ic_corrct,0);
  8. editText.setCompoundDrawablePadding(5);}

猜你在找的Android相关文章