android - 两个 drawable to edittext

标签 android android-edittext android-drawable

我已经在 edittext 的两端添加了可绘制对象。当我点击右边的 drawable 时,左边的就消失了。 基本上,edittext 用于输入密码。图标锁定和图标可见性(眼睛)分别位于 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

最佳答案

您正在为 EditText etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0); 的开始|左图标位置添加 0;

在 EditText 的开始|左边位置添加你的 lock_icon :

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

最终代码:

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(lock_icon, 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(lock_icon, 0, R.drawable.ic_visibility_white_24dp, 0);

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

关于android - 两个 drawable to edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38824042/

相关文章:

javascript - 当插件 Media 是项目的一部分时,Ripple Cordova Chrome "aw snap"消息

android - 在 Android 的 ListView 中单击一个项目

android - EditText 错误图标并显示密码放错地方

java - 我的测验应用程序没有给出正确的结果并转到最终结果

Android Drawable Layer 列表项颜色访问

android - 如何在运行时自定义以 XML 定义的自定义可绘制对象?

java - 在 Android 中解析 JSON 字符串的更快方法

iphone - 是否有适用于 iPhone、Android 和 Windows Phone 7 的增强现实框架?

android - 无法控制编辑文本中的光标位置

android - 在 ExpandableListView 中对父项和子项使用自定义选择器