当前位置:  开发笔记 > Android > 正文

在编辑文本中可绘制,在错误后不更新

如何解决《在编辑文本中可绘制,在错误后不更新》经验,为你挑选了0个好方法。

在android中编辑文本不允许在setError之后更改drawable.我在密码字段中使用了drawable right,但如果密码字段中出现错误,则不允许在其后更改drawable.错误之前它工作正常.


用于更改眼睛图标运行时的Java代码

private void setPasswordDrawable()
    {
        final Drawable showpass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility);
        final Drawable hidepass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility_off);


        final Drawable pass_drawable = getResources().getDrawable(R.mipmap.ic_action_password);
        pass_drawable.setBounds(0, 0, pass_drawable.getIntrinsicWidth(), pass_drawable.getIntrinsicHeight());


        //edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);

        edtPassword.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (edtPassword.getCompoundDrawables()[2] == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > edtPassword.getWidth() - edtPassword.getPaddingRight()
                        - showpass_icon.getIntrinsicWidth()) {

                    if (isPasswordVisible) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //edtPassword.setError(null);
                                edtPassword.setTransformationMethod(
                                        PasswordTransformationMethod.getInstance());
                                edtPassword.setSelection(edtPassword.getText().length());

                                showpass_icon.setBounds(0, 0, showpass_icon.getIntrinsicWidth(), showpass_icon.getIntrinsicHeight());
                                edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);


                            }
                        });

                        isPasswordVisible = false;
                    } else {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //edtPassword.setError(null);
                                edtPassword.setTransformationMethod(
                                        HideReturnsTransformationMethod.getInstance());
                                edtPassword.setSelection(edtPassword.getText().length());
                                hidepass_icon.setBounds(0, 0, hidepass_icon.getIntrinsicWidth(), hidepass_icon.getIntrinsicHeight());
                                edtPassword.setCompoundDrawables(pass_drawable, null, hidepass_icon, null);
                            }
                        });

                        isPasswordVisible = true;
                    }
                }
                return false;
            }
        });

    }

用于设置错误

public void setViewError(View view, String message)
{
    if (view instanceof EditText) {
         ((EditText) view).setError(message);
   }
}

实例

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有