两个drawable到edittext

时间:2016-08-08 08:02:31

标签: android android-edittext android-drawable

我在edittext的两端添加了drawable。当我点击右侧可绘制时,左侧消失。 基本上,edittext用于密码输入.Icon锁定和图标可见性(眼睛)分别位于edittext的左侧和右侧。右侧图标根据密码的可见性切换

可绘制的侦听器实现:

 etPassword.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_RIGHT = 2;

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
                            etPassword.setInputType(InputType.TYPE_CLASS_TEXT |
                                    InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);
                            etPassword.setSelection(etPassword.getText().length());
                        } else {
                            etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                            etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_white_24dp, 0);

                        }
                        return true;
                    }
                }
                return false;
            }
        });

XML

 <EditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@drawable/et_bg"
                android:drawableRight="@drawable/ic_visibility_off_white_24dp"
                android:hint="@string/password"
                android:drawableLeft="@drawable/ic_lock_white_24dp"
                android:inputType="textPassword"
                android:maxLines="1"
                android:padding="10dp"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/textColorPrimary" />

截屏: enter image description here

问题在于点击可见性图标,左侧可绘制消失,此处是屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:2)

您为EditText <profiles> <profile> <id>guvnor-m2-repo</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>guvnor-m2-repo</id> <name>Guvnor M2 Repo</name> <url>http://10.10.10.57:8080/kie-wb/maven2</url> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles>

的开始|左侧图标位置添加0

将您的lock_icon添加到EditText的开始|左侧位置:

etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);

最终代码:

etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0);