Edittext InputLayout.setError 仅在第一次显示

Editext InputLayout.setError showing only at the first time

我正在尝试 validate 通过 TextWatcher 来自 Editext 的输入并显示 error 及其 InputLayout。 问题是,InputLayout error is showing at the first time only 下一次输入无效时,InputLayout 错误不再显示。

我只是想验证输入的端口,我希望它从 2000 到 65535 开始并且我的布局显示为对话框。

final Dialog dialog = new Dialog(PanelActivity.this,R.style.CustomDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.port_layout);
final EditText ePort = (EditText) dialog.findViewById(R.id.input_port);
final TextInputLayout inputLayoutPort = (TextInputLayout)dialog.findViewById(R.id.input_layout_port);

ePort.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() > 1){
                    if(Integer.parseInt(String.valueOf(s)) < 2000 || Integer.parseInt(String.valueOf(s)) > 65535){
                        inputLayoutPort.setError("Port must be 2000 to 65535.");
                        //Inputted port is incorrect
                    }else{
                        inputLayoutPort.setErrorEnabled(false);
                        .... //Inputted port is correct
                    }
                }else{
                    inputLayoutPort.setError("Port must be 2000 to 65535.");
                    //Inputted port is incorrect
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

dialog.show();

我已经尝试将其更改为,尽管它会起作用。

... //onTextChanged
        if(s.length() > 1){
            if(Integer.parseInt(String.valueOf(s)) < 2000 || Integer.parseInt(String.valueOf(s)) > 65535){
                inputLayoutPort.setError("Port must be 2000 to 65535.");
                inputLayoutPort.setErrorEnabled(true);
                //Inputted port is incorrect
            }else{
                inputLayoutPort.setErrorEnabled(false);
                .... //Inputted port is correct
            }
        }else{
            inputLayoutPort.setError("Port must be 2000 to 65535.");
            inputLayoutPort.setErrorEnabled(true);
            //Inputted port is incorrect
        }

但是,第一次显示只是输入无效

这是我第一次使用 InputLayout,我只是按照我找到的教程进行操作,但我似乎遗漏了一些东西,我无法弄清楚。

更新: 以下代码适用于我的 Inputlayout xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    //....

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_port"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_below="@+id/defaultValue"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_centerHorizontal="true">

            <EditText
                android:id="@+id/input_port"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Server Port"
                android:inputType="number"
                android:maxLength="5"
                android:nextFocusLeft="@id/input_port"
                android:nextFocusUp="@id/input_port"
                android:textAlignment="center"
                android:textStyle="bold" />

        </android.support.design.widget.TextInputLayout>

    //...
</RelativeLayout>

根据您的代码,您可以将 onTextChanged() 简化为:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    inputLayoutPort.setError("Port must be 2000 to 65535.");

    if(s.length() > 1 && !(Integer.parseInt(String.valueOf(s)) < 2000 || Integer.parseInt(String.valueOf(s)) > 65535)) {
        inputLayoutPort.setError(null);
    }
}

因此,每次编辑文本中的文本更改时,我都会设置错误消息“Port must be 2000 to 65535.”。然后,结合您的两个条件 s.length() > 1s lies between 2000 and 65535,我通过将错误设置为空来删除错误消息。

我最近发现了同样的问题。

使用 inputLayoutPort.setErrorEnabled(false) 会使 TextInputLayout 处于无法再次打开错误的状态。我不确定这是否是预期行为,因为文档不清楚。

执行此操作的正确方法似乎是只调用 inputLayoutPort.setError(null),因为这将清除错误。 记录的行为,所以我同意了。