android TextInputLayout.class.getDeclaredField ("mErrorView") выдает исключение - PullRequest
0 голосов
/ 25 января 2020

Я хочу, чтобы мой TextInputlayout setError находился справа от вида. Я пытался с CustomTextInputlayout.TextInputLayout.class.getDeclaredField ("mErrorView") выдает исключение. MyProject с библиотекой androidX.

java .lang.NoSuchFieldException: нет поля mErrorView в классе Lcom / google / android / material / textfield / TextInputLayout;

publi c Класс CustomTextInputLayout extends TextInputLayout {

public CustomTextInputLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setErrorEnabled(boolean enabled) {
    super.setErrorEnabled(enabled);

    if (!enabled) {
        return;
    }

    try {
        Field errorViewField = TextInputLayout.class.getDeclaredField("mErrorView"); // Getting exception here
        errorViewField.setAccessible(true);
        TextView errorView = (TextView) errorViewField.get(this);
        if (errorView != null) {
            errorView.setGravity(Gravity.RIGHT);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.END;
            errorView.setLayoutParams(params);
        }
    } catch (Exception e) {
        // At least log what went wrong
        e.printStackTrace();
    }
}

}

1 Ответ

1 голос
/ 25 января 2020

Полагаю, вы делаете это неправильно. Существует поле с именем IndicatorViewController, которое контролирует ошибки и показывает вспомогательный текст. Версия материала: 1.1.0-alpha02. Проверьте исходный код TextInputLayout.

Появился текст ошибки. Вы можете изменить гравитацию по стилю (R.styleable.TextInputLayout_errorTextAppearance).

...