2016-08-03 59 views
7

我试图在输入类型为文本密码的编辑文本中显示用户输入的密码。编辑文本密码切换Android

我实现gesturelistener在切换图标像这个 -

public boolean onTouch(View view, MotionEvent motionEvent) { 
     switch (view.getId()) 
     { 
      case R.id.ivPasswordToggle: 

       switch (motionEvent.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show(); 
         etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
         break; 
        case MotionEvent.ACTION_UP: 
         etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); 
         Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show(); 
         break; 
       } 
       break; 
     } 
     return true; 
    } 

我不知道什么是错的,任何帮助将不胜感激。

+0

我的祝酒词都工作正常,但在编辑文本 –

回答

14

自支持库v24.2.0。你可以achivie这很容易

您需要做的仅仅是:

  1. 设计库一起添加到您的依赖条件

    dependencies { 
        compile "com.android.support:design:25.1.0" 
    } 
    
  2. 使用TextInputEditTextTextInputLayout

    <android.support.design.widget.TextInputLayout 
        android:id="@+id/etPasswordLayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:passwordToggleEnabled="true"> 
    
        <android.support.design.widget.TextInputEditText 
         android:id="@+id/etPassword" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/password_hint" 
         android:inputType="textPassword"/> 
    </android.support.design.widget.TextInputLayout> 
    

passwordToggleEnabled属性将密码切换显示

  • 在你的根布局不要忘记添加xmlns:app="http://schemas.android.com/apk/res-auto"

  • 您可以通过使用自定义密码切换:

  • app:passwordToggleDrawable - 可绘制用作密码输入可见性切换图标。
    app:passwordToggleTint - 用于密码输入可见性切换的图标。
    app:passwordToggleTintMode - 用于应用背景色调的混合模式。

    更多详细信息在TextInputLayout documentation

    enter image description here

    +0

    默认我想密码不可见。我如何做到这一点 –

    1

    请尝试以下方法。在这里,我们正在设置一个复合绘图,当点击时将显示或隐藏密码:

    private boolean passwordShown = false; 
    
    private void addPasswordViewToggle() { 
         getPasswordEditText().setOnTouchListener(new View.OnTouchListener() { 
          @Override 
          public boolean onTouch(View v, MotionEvent event) { 
           final int DRAWABLE_RIGHT = 2; //index 
    
           if (event.getAction() == MotionEvent.ACTION_UP) { 
            if (event.getRawX() >= (getPasswordEditText().getRight() - getPasswordEditText().getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
             if (passwordShown) { 
              passwordShown = false; 
              // 129 is obtained by bitwise ORing InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD 
              getPasswordEditText().setInputType(129); 
    
              // Need to call following as the font is changed to mono-space by default for password fields 
              getPasswordEditText().setTypeface(Typeface.SANS_SERIF); 
              getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.locked_icon, 0); // This is lock icon 
             } else { 
              passwordShown = true; 
              getPasswordEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
    
              getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unlocked_icon, 0); // Unlock icon 
             } 
    
             return true; 
            } 
           } 
           return false; 
          } 
         }); 
        } 
    
    6

    请尝试此代码。

    public boolean onTouch(View view, MotionEvent motionEvent) { 
         switch (view.getId()) 
         { 
          case R.id.ivPasswordToggle: 
    
           switch (motionEvent.getAction()) { 
            case MotionEvent.ACTION_DOWN: 
             Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show(); 
             etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 
             break; 
            case MotionEvent.ACTION_UP: 
             etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
             Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show(); 
             break; 
           } 
           break; 
         } 
         return true; 
        } 
    

    我希望它能工作,谢谢。

    +0

    的文本没有影响thanks.the代码工作正常,但每次我点击绘制编辑文本光标转到第一位置 –

    +0

    尝试这两个开关例子..... etPassword.setSelection(etPassword.getText()。length());让我知道,如果它不起作用。 – 2016-08-03 05:58:11

    +0

    我得到了答案,我不得不添加edittext.setSelection(Position)。在内部,采取行动和采取行动。 –