如何在显示TextInputLayout错误时禁用浮动效果

6
<android.support.design.widget.TextInputLayout
        android:id="@+id/productLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true">

  <EditText
      android:id="@+id/product"
      android:layout_width="match_parent"
      android:layout_height="@dimen/margin_padding_width_height_6"
      android:cursorVisible="false"
      android:drawableRight="@drawable/ic_arrow_down"
      android:focusableInTouchMode="false"
      android:hint="@string/product"
      android:inputType="none"
      android:paddingEnd="@dimen/margin_padding_width_height_2"
      android:paddingRight="@dimen/margin_padding_width_height_2"
      android:singleLine="true"
      android:textSize="@dimen/text_size_m" />

private boolean validateFields() {
        if (mCategory.getText().toString().isEmpty())
            mCategoryLayout.setError("Please select a category");
        else if (mProducts.getText().toString().isEmpty())
            mProductsLayout.setError("Please select a product");
        else if (mSerialNumber.getText().toString().isEmpty())
            mSerialNumberLayout.setError("Please enter the serial number");
        else
            return true;
        return false;
    }

我已经为EditText实现了点击监听器,所以当在TextInputLayout上设置错误时,我不想将EditText标签浮动到顶部。我该如何禁用它?


标签动画 - http://www.androidhive.info/2015/09/android-material-design-floating-labels-for-edittext/ - Vaisakh N
1
从文档中看,我猜您需要在程序中使用setHintAnimationEnabled(false)方法或在xml中使用android.support.design:hintAnimationEnabled="false"来禁用TextInputLayout的提示动画。 - Opiatefuchs
动画消失了,但标签仍然浮动在顶部。 - Vaisakh N
你是否在Gradle中使用相同版本编译,例如:compile 'com.android.support:design:23.0.0'? - Opiatefuchs
所以肯定还有其他问题...请更新您的代码,展示您是如何禁用它的。我认为如果您使用的minSDK构建版本小于23m,它将无法工作......我猜... - Opiatefuchs
显示剩余4条评论
4个回答

4

这将移除提示视图,因此布局将向上移动。 - artem

3

如果有人想要保留浮动标签,但在使用TextInputLayoutsetError()时不要折叠它们,我发现了一个可行的解决方案,适用于支持25.4.0的版本。不幸的是,在更新的版本中可能会出现错误。

public class HackedTextInputLayout extends TextInputLayout {

    private int forceExpandedHintBlockFlags = 0;

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);

        if (child instanceof EditText) {
            ((EditText) child).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 (!TextUtils.isEmpty(getError()) && TextUtils.isEmpty(s)) {
                        forceExpandedHintBlockFlags++;
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(getError()) && TextUtils.isEmpty(s)) {
                        forceExpandedHintBlockFlags--;
                    }
                }
            });
        }
    }

    @Override
    public void setError(@Nullable CharSequence error) {
        forceExpandedHintBlockFlags++;
        super.setError(error);
        forceExpandedHintBlockFlags--;
    }

    @Nullable
    @Override
    public CharSequence getError() {
        // the main hack is right here
        return forceExpandedHintBlockFlags > 0 ? null : super.getError();
    }

    @Override
    public void setHintTextAppearance(int resId) {
        forceExpandedHintBlockFlags++;
        super.setHintTextAppearance(resId);
        forceExpandedHintBlockFlags--;
    }

    @Override
    protected void drawableStateChanged() {
        forceExpandedHintBlockFlags++;
        super.drawableStateChanged();
        forceExpandedHintBlockFlags--;
    }
}

主要的hack是基于TextInputLayout源代码。在setError()TextWatcher和其他一些函数中,会调用updateLabelState()函数,在该函数中有以下条件:

if (hasText || this.isEnabled() && (isFocused || isErrorShowing)) {
    if (force || this.mHintExpanded) {
        this.collapseHint(animate);
    }
} ...

这里的关键是 isErrorShowing = !TextUtils.isEmpty(this.getError()) 的值。所以,如果我们暂时禁用 getError(),那么在那个位置上我们总是有 isErrorShowing == false :)


0
试试这个:
mCategoeyLayout.setHintAnimationEnabled(false);

稍后(比如在点击Edittext时)
mCategoeyLayout.setHintAnimationEnabled(true);

-1
  private boolean validateFields() {
    if (mCategory.getText().toString().isEmpty())
        mCategoryLayout.setError("Please select a category");
    else if (mProducts.getText().toString().isEmpty())
        mProductsLayout.setError("Please select a product");
    else if (mSerialNumber.getText().toString().isEmpty())
        mSerialNumberLayout.setError("Please enter the serial number");
    else
        return true;
    return false;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接