2016-08-08 65 views
0

Ich habe Zeichen an beiden Enden von Editiertext hinzugefügt. Wenn ich nach rechts ziehbar klicke, verschwindet der linke. Grundsätzlich ist die EditText ist für Passwort input.Icon sperren und Symbol Sichtbarkeit (Auge) sind nach links und rechts von EditText respectivily.The rechte Symbol schaltet nach Sichtbarkeit PasswortZwei Zeichen zum Bearbeiten von Text

Listener-Implementierung für Recht ziehbar:

etPassword.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       final int DRAWABLE_RIGHT = 2; 

       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
         if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { 
          etPassword.setInputType(InputType.TYPE_CLASS_TEXT | 
            InputType.TYPE_TEXT_VARIATION_PASSWORD); 
          etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0); 
          etPassword.setSelection(etPassword.getText().length()); 
         } else { 
          etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
          etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_white_24dp, 0); 

         } 
         return true; 
        } 
       } 
       return false; 
      } 
     }); 

xml:

<EditText 
       android:id="@+id/etPassword" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/et_bg" 
       android:drawableRight="@drawable/ic_visibility_off_white_24dp" 
       android:hint="@string/password" 
       android:drawableLeft="@drawable/ic_lock_white_24dp" 
       android:inputType="textPassword" 
       android:maxLines="1" 
       android:padding="10dp" 
       android:singleLine="true" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="@color/textColorPrimary" /> 

Screenshots: enter image description here

Das Problem ist auf Sicht Symbol klicken, die linke Seite ziehbar verschwindet, hier ist der Screenshot enter image description here

+0

Was ist das Problem> –

+0

rechten Symbol ist zum Umschalten des Show-Passwort und ausblenden. Was ist das linke Symbol für ??? –

+0

@Sagar Nayak hat Screenshots – musica

Antwort

1

Sie fügen 0 für Start | links Symbol Position EditText etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);

Fügen Sie Ihre lock_icon in Start | linke Position Ihrer EditText:

etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0); 

Schlusscode:

etPassword.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       final int DRAWABLE_RIGHT = 2; 

       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
         if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { 
          etPassword.setInputType(InputType.TYPE_CLASS_TEXT | 
            InputType.TYPE_TEXT_VARIATION_PASSWORD); 
          etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0); 
          etPassword.setSelection(etPassword.getText().length()); 
         } else { 
          etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
          etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_white_24dp, 0); 

         } 
         return true; 
        } 
       } 
       return false; 
      } 
     });