EditText error icon and show password missplaced

空扰寡人 提交于 2019-12-17 02:51:03

问题


I have an EditText as password input like this

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:singleLine="true" />

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

It's working, but when there's an error the error icon shwon twice and it's on top of show password icon.

My validation code to show the error :

if (success) {
    finish();
    startMainActivity();
} else {
         mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
}

回答1:


Don't call setError on the EditText, use TextInputLayout's setError()




回答2:


Same behavior for material version 1.1.0-alpha10, even if you set an error into TextInputLayout. You can avoid it by adding to the TextInputLayout this line :

app:errorIconDrawable="@null"




回答3:


With this code, you can remove toggle while showing the error. you can show toggle while user writing anything. Don't remember, you should give id to text input layout

public void showError(){
    password.setError(errorMessage);
    password.requestFocus();
    textInputLayout.setPasswordVisibilityToggleEnabled(false);
}


 password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                textInputLayout.setPasswordVisibilityToggleEnabled(true);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });



回答4:


It seems that it is a bug after updating gradle dependencies to 24+. Please, check this answer. I had all my setError() working fine before that. Plus, you hadn't to ask for focus in order to display the error.



来源:https://stackoverflow.com/questions/39313026/edittext-error-icon-and-show-password-missplaced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!