【问题标题】:Drawable right in edit text not updating after error编辑文本中的可绘制权在错误后未更新
【发布时间】:2016-04-15 12:17:28
【问题描述】:

在 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);
   }
}

【问题讨论】:

  • 你得到答案了吗?
  • @PriyankaAlachiya 未尝试使用最新的支持库。用其他方式完成。

标签: android android-edittext saripaar


【解决方案1】:

可能已经很晚了。我也遇到过这类问题。我很沮丧地找到了解决方案。最后我找到了解决方案。它可能对像我这样的人有所帮助。解决方法很简单。

EditText 会在我们设置错误时存储错误,即使输入正确的值也不会自动删除。所以你需要手动覆盖错误文本并设置空值。

    if (!isError) {
        edt_reg_password.setError("");
        edt_reg_password.setText(edt_reg_password.getText().toString());
    } else {
        edt_reg_password.setError("Data Invalid");
    }

谢谢!快乐编码!

【讨论】:

    【解决方案2】:

    你可以这样使用 -

    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);}
    

    【讨论】:

    • 它不工作,我也试过这个。我认为一旦 android 设置错误 drwable 它就不允许其他可绘制来替换它。我什至也尝试过 setError(null)。
    • 你的最小和目标 sdk 是多少?
    • minSdkVersion 15 , targetSdkVersion 23 y ?这重要吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多