为什么Android EditText必须第二次点击才能触发事件?

时间:2017-03-30 02:37:03

标签: android

xml文件:

<EditText
        android:drawableLeft="@drawable/icon_lock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:id="@+id/et_pwd"
        android:drawableRight="@drawable/icon_closeeye_32"
        style="@style/editText_base"
        android:hint="@string/pwd_input"/>

外部是LinearLayout

代码:

private boolean isTouch = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            Drawable drawableRight = etPwd.getCompoundDrawables()[2];

            if(drawableRight == null && event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            if (event.getX() > etPwd.getWidth()
                    - etPwd.getPaddingRight()
                    - drawableRight.getIntrinsicWidth()){
                Drawable drawableLeft = getResources().getDrawable(R.drawable.icon_lock);
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                if (isTouch) {
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    Drawable drawableOpenEye = getResources().getDrawable(R.drawable.icon_openeye_32);
                    drawableOpenEye.setBounds(0, 0, drawableOpenEye.getMinimumWidth(), drawableOpenEye.getMinimumHeight());
                    etPwd.setCompoundDrawables(drawableLeft, null, drawableOpenEye, null);
                } else {
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    Drawable drawableCloseEye = getResources().getDrawable(R.drawable.icon_closeeye_32);
                    drawableCloseEye.setBounds(0, 0, drawableCloseEye.getMinimumWidth(), drawableCloseEye.getMinimumHeight());
                    etPwd.setCompoundDrawables(drawableLeft, null, drawableCloseEye, null);
                }
                isTouch = !isTouch;
                etPwd.setSelection(etPwd.getText().toString().length());
            }

            break;
        case MotionEvent.ACTION_MOVE :

            break;
        case MotionEvent.ACTION_UP :

            break;
    }

    return false;
}

Now! Did not respond for the first time , Click the second time to show the icon open , to show / hide the password.

我已经实现了View.OnTouchListener,找到了执行click事件的相关控件。帮助我,谢谢!

2 个答案:

答案 0 :(得分:1)

您需要更改

private boolean isTouch = false;

为真。

private boolean isTouch = true;

或者您需要在if语句中切换块。

原因是因为第一次通过代码块时,isTouch为false,并将转换设置为密码 - 它已经存在!现在isTouch设置为true,第二次通过预期的代码块来显示密码。

答案 1 :(得分:0)

试试这个,

XML:

<RelativeLayout
        android:id="@+id/relative01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_username"
        android:background="@drawable/selector"
        android:padding="10dp">

        <com.mycalculator.utils.CustomEditText
            android:id="@+id/et_pwd"
            android:layout_width="fill_parent"
            android:layout_height="42dp"
            android:layout_toStartOf="@+id/iv_show_pwd"
            android:background="@null"
            android:hint="@string/Password"
            android:inputType="textPassword"
            android:singleLine="true"
            android:textColor="@color/colorBlack"
             />

        <ImageView
            android:id="@+id/iv_show_pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="3dp"
            android:layout_marginStart="3dp"
            android:src="@drawable/icon_closeeye_32" />
</RelativeLayout>

选择器:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="6dp" />

    <gradient
        android:angle="90" />
    <solid android:color="@color/colorWhite"/>
    <stroke
        android:width="1dp"
        android:color="@color/Gray" />

</shape>

JAVA:

EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
ImageView iv_show_pwd = (ImageView) findViewById(R.id.iv_show_pwd);


iv_show_pwd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (et_pwd.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            iv_show_pwd.setImageResource(R.drawable.ic_visible);
        } else {
            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            iv_show_pwd.setImageResource(R.drawable.ic_invisible);
        }
    }
});