我在编辑文本中添加了图像,用于显示和隐藏密码。如何执行 单击操作以在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 :(得分: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);
答案 1 :(得分: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);
}
}
只需在XML&amp;中替换您的EditText即可。把图像放在
里面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;
}
}
});
希望这会对你有所帮助