2016-05-06 44 views
-1

我添加圖片編輯文本內對顯示和隱藏password..how上點擊操作執行 用於示出並在內部EDITTEXT隱藏密碼該圖像顯示和隱藏密碼的ImageButton內部的EditText

下面

是mycode的

<EditText 
    android:layout_width="170dp" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:layout_marginBottom="7dp" 
    android:background="@drawable/ic_icon" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:id="@+id/routerPassWd" 
    android:textColor="@color/black" 
    android:layout_column="2" 
    android:layout_row="5" 
    android:hint="Enter Passwd"/> 
+0

檢查這個演示:http://www.codeproject.com/Tips/518641/Show-hide-password-in-a-edit-text-view-password-ty。併發布您的代碼 –

+0

您應該包含代碼,以便人們可以提供幫助。它也顯示了一些努力被消耗。 – MikeT

+0

你明白我的問題..我問如果地方Image按鈕裏面edittext的那個東西如何執行點擊操作 – pruthvi

回答

0

嘗試這

  // show password 
      etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

      // hide password 
      etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance() 



    // to show try this another way 


     editText.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL); 



    // to hide 

     editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
+0

onClick ..不工作... – pruthvi

+0

試試這個我編輯回答 –

+0

爲隱藏試試這個 editText。 setInputType(129); –

0

據我瞭解你在EditText上並在該圖像的添加映像點擊你想要顯示/隱藏文字轉換。如果是這樣,那麼你必須創建一個自定義的EditText,它會爲你提供點擊的圖像。以下是爲您提供圖像的點擊偵聽器的EditText的代碼。你可以在你的代碼中使用它並相應地切換轉換。

public class DrawableEditText extends EditText { 
private Drawable left; 
private Drawable top; 
private Drawable right; 
private Drawable bottom; 
private int actionX; 
private int actionY; 
private DrawableClickListener listener; 

public DrawableEditText(Context context) { 
    super(context); 
} 

public DrawableEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public DrawableEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { 
    this.left = left; 
    this.top = top; 
    this.right = right; 
    this.bottom = bottom; 
    super.setCompoundDrawables(left, top, right, bottom); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    Rect bounds; 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     actionX = (int) event.getX(); 
     actionY = (int) event.getY(); 
     if (bottom != null && bottom.getBounds().contains(actionX, actionY)) { 
      listener.onClick(this, DrawableClickListener.Position.BOTTOM); 
     } else if (top != null && top.getBounds().contains(actionX, actionY)) { 
      listener.onClick(this, DrawableClickListener.Position.TOP); 
     } else if (left != null) { 
      bounds = null; 
      bounds = left.getBounds(); 
      int x, y; 
      int extraTapArea = (int) (13 * getResources().getDisplayMetrics().density + 0.5); 
      x = actionX; 
      y = actionY; 
      if (!bounds.contains(actionX, actionY)) { 
       x = (int) (actionX - extraTapArea); 
       y = (int) (actionY - extraTapArea); 
       if (x <= 0) 
        x = actionX; 
       if (y <= 0) 
        y = actionY; 
       if (x < y) { 
        y = x; 
       } 
      } 
      if (bounds.contains(x, y) && listener != null) { 
       listener.onClick(this, DrawableClickListener.Position.LEFT); 
       event.setAction(MotionEvent.ACTION_CANCEL); 
       return false; 
      } 
     } else if (right != null) { 
      bounds = null; 
      bounds = right.getBounds(); 
      int x, y; 
      int extraTapArea = 13; 
      x = (int) (actionX + extraTapArea); 
      y = (int) (actionY - extraTapArea); 
      x = getWidth() - x; 
      if (x <= 0) { 
       x += extraTapArea; 
      } 
      if (y <= 0) 
       y = actionY; 
      if (bounds.contains(x, y) && listener != null) { 
       listener.onClick(this, DrawableClickListener.Position.RIGHT); 
       event.setAction(MotionEvent.ACTION_CANCEL); 
       return false; 
      } 
     } 
    } 
    return super.onTouchEvent(event); 
} 

public void setOnDrawableClickListener(DrawableClickListener l) { 
    listener = l; 
} 

public static interface DrawableClickListener { 

    public static enum Position { 
     LEFT, TOP, RIGHT, BOTTOM 
    } 

    void onClick(View v, Position position); 
} 
} 

只是這個替換您的EditText在你的XML &地方,圖像內

android:drawableRight="@drawable/ic_icon" 

裏面你的Java文件使用:

DrawableEditText editText = (DrawableEditText) findViewById(R.id.<your id>); 
editText.setOnDrawableClickListener(new DrawableClickListener() { 
    @Override 
    public void onClick(View v, Position position) { 
     switch (position) { 
      case RIGHT: 
       //TODO code to switch between transformation 
       break; 
     } 
    } 
}); 

希望這將幫助你