在edittext中显示和隐藏Imagebutton的密码



我在编辑文本中添加了图像,用于显示和隐藏密码。。如何执行点击操作,在内部编辑文本上显示和隐藏该图像的密码

下面是分枝杆菌

 <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"/>

根据我的理解,您在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);
}
}

只需在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;
        }
    }
});

希望这将帮助您

尝试此

            // 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);

最新更新