據我瞭解你在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;
}
}
});
希望這將幫助你
檢查這個演示:http://www.codeproject.com/Tips/518641/Show-hide-password-in-a-edit-text-view-password-ty。併發布您的代碼 –
您應該包含代碼,以便人們可以提供幫助。它也顯示了一些努力被消耗。 – MikeT
你明白我的問題..我問如果地方Image按鈕裏面edittext的那個東西如何執行點擊操作 – pruthvi