编辑文本中的图形右边在错误后不会更新

问题描述 投票:7回答:2

在android中编辑文本不允许在setError之后更改绘图能力。我已经在密码字段中使用了可绘制权限,但如果密码字段中出现错误,将不允许更改其绘制能力。在出错之前,它可以正常工作。

<EditText
                android:id="@+id/edt_reg_password"
                style="@style/editText_full_view"
                android:layout_height="wrap_content"
                android:layout_below="@id/edt_reg_email"
                android:layout_marginTop="@dimen/padding_normal"
                android:drawableLeft="@mipmap/ic_action_password"
                android:drawableRight="@mipmap/ic_action_password_visibility"
                android:drawablePadding="@dimen/padding_normal"
                android:hint="@string/hint_password"
                android:inputType="textPassword"
                android:maxLength="25"
                android:paddingLeft="@dimen/padding_normal"
                tools:visibility="visible" />

用于更改眼睛图标运行时间的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);
   }
}

live example

android android-edittext saripaar
2个回答
0
投票

您可以这样使用-

if(error=true){
    editText.setCompoundDrawablesWithIntrinsicBounds(
        0, 0,R.drawable.ic_error, 0);
    editText.setCompoundDrawablePadding(5);}
else{
    editText.setCompoundDrawablesWithIntrinsicBounds(
        0, 0,R.drawable.ic_corrct, 0);
    editText.setCompoundDrawablePadding(5);}

0
投票

太晚了,但对于那些通过谷歌搜索访问此页面的人:您必须用TextInputLayout包裹EditText并为textInputLayout设置错误,而不是editText

© www.soinside.com 2019 - 2024. All rights reserved.