0

嗨,我正在使用 TextInputLayout 和 TextInputEditText 像这样

<android.support.design.widget.TextInputLayout
    android:id="@+id/userName_til"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorTextAppearance="@style/error_appearance">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/etUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/username"
            android:inputType="textEmailAddress"
            android:maxLines="1" />

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

风格是

<style name="error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@android:color/holo_red_light</item>
    <item name="android:textSize">12sp</item>
</style>

Java 代码是

tilUserName = (TextInputLayout) findViewById(R.id.userName_til);
etUserName = (TextInputEditText) findViewById(R.id.etUserName);

etUserName.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) {
        Timber.d("OnTextChanged Called");
        if (s.length() < 1) {
            tilUserName.setErrorEnabled(true);
            tilUserName.setError(getString(R.string.please_enter_email_id));
        } else if (!AppUtils.isValidEmail(s)) {
            tilUserName.setErrorEnabled(true);
        tilUserName.setError(getString(R.string.please_enter_valid_email_id));
        } else {
            tilUserName.setError(null);
            tilUserName.setErrorEnabled(false);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

我在用...

 compile 'com.android.support:appcompat-v7:25.2.0'
 compile 'com.android.support:design:25.2.0' 

参考图像肖像 虽然正常时间的图像

风景改变后 横向模式错误显示时间

在方向更改时自动显示 EditText 中的错误提示。

我该如何解决这个问题?

4

2 回答 2

1

尝试添加TextWatcherin aRunnable以便在 android 恢复您的 edittext 状态后附加它。像这样的东西:

tilUserName = (TextInputLayout) findViewById(R.id.userName_til);
etUserName = (TextInputEditText) findViewById(R.id.etUserName);
etUserName.post(new Runnable() {
        @Override
        public void run() {
            etUserName.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) {
                Timber.d("OnTextChanged Called");
                if (s.length() < 1) {
                    tilUserName.setErrorEnabled(true);
                    tilUserName.setError(getString(R.string.please_enter_email_id));
                } else if (!AppUtils.isValidEmail(s)) {
                    tilUserName.setErrorEnabled(true);
                    tilUserName.setError(getString(R.string.please_enter_valid_email_id));
                } else {
                    tilUserName.setError(null);
                    tilUserName.setErrorEnabled(false);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
};

引用自Android - onTextChanged() 在手机方向改变时调用

于 2017-03-03T12:07:51.953 回答
0

因为 onTextChange() 在旋转屏幕时调用(也许:D)

当用户单击完成时,您应该检查有效。

于 2017-03-03T10:23:57.743 回答