首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >两个可绘图的编辑文本

两个可绘图的编辑文本
EN

Stack Overflow用户
提问于 2016-08-08 08:02:31
回答 1查看 1.7K关注 0票数 1

我在编辑文本的两端都增加了可绘制的内容。当我点击右侧可绘制时,左边的一个消失了。基本上,编辑文本是用于密码的input.Icon锁和图标可见性(眼睛)在编辑文本的左边和右边,respectivily.The右图标切换为密码的可见性。

正确可绘制的侦听器实现:

代码语言:javascript
运行
复制
 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

代码语言:javascript
运行
复制
 <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" />

屏幕截图:

问题是点击可见性图标,左边的可绘制消失,这是屏幕截图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-08 10:00:21

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

将您的lock_icon添加到EditText的start_left位置:

代码语言:javascript
运行
复制
etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0);

最终代码:

代码语言:javascript
运行
复制
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;
            }
        });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38824042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档