【问题标题】:How to set the error text alignment to center in TextInputLayout如何在 TextInputLayout 中将错误文本对齐设置为居中
【发布时间】:2021-08-20 01:55:50
【问题描述】:

如何将错误文本消息对齐到edittext的中心和下方。我尝试对齐但它不起作用。这是我的代码:

Xml

<android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tilEmail">
 <EditText
                    android:layout_width="match_parent"
                    android:id="@+id/mob"
                    android:layout_height="35dp"
                    android:background="@drawable/round_corner_3"
                    android:inputType="phone"
                    android:hint="Mobile Number"
                    android:textColorHint="@color/colorGray"
                    android:textColor="@color/Black"
                        android:drawablePadding="10dp"/>
                </android.support.design.widget.TextInputLayout>

Java 类

til = (TextInputLayout) findViewById(R.id.tilEmail);

                     til.setError("Mobile number not valid");

                     til.setError(Html.fromHtml("<font color='red'>Mobile number not valid</font>"));
                    mobile.setError(null);

         mobile.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                    Toast.makeText(getApplicationContext(), "Phone number or Password is not valid", Toast.LENGTH_SHORT).show();

                }

【问题讨论】:

标签: android textinputlayout


【解决方案1】:
// reference an error textview
TextView textView = (TextView) til.findViewById(android.support.design.R.id
            .textinput_error);

if (textView != null) {
    // can be RelativeLayout/FrameLayout Params, depending on your xml
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
    params.gravity = Gravity.CENTER|Gravity.BOTTOM;
    textView.setLayoutParams(params);
}

另外,您可以为LayoutParams添加边距

【讨论】:

  • 你为什么使用TextView?
  • 好吧,在这种特殊情况下,您可以概括为简单的View。还是我误解了你的问题?
  • @Boris Kozyrev 我希望该错误消息显示在编辑文本框下方的中心,但它从开始显示
  • @pratival,那么您是否尝试过从 TextInputLayout 获取错误文本视图并使用我提供的代码片段将其参数的重心设置为中心?
  • @BorisKozyrev 根本不起作用。您还有其他解决方案吗?
【解决方案2】:

您需要创建一个扩展TextInputLayout 的自定义类并在那里设置重力。这是因为在先前设置错误之前您无法获得 R.id.textinput_error_text id。

使用 Kotlin:

class TextInputLayoutErrorEnd(context: Context, attrs: AttributeSet) :
    TextInputLayout(context, attrs) {
    override fun setError(errorText: CharSequence?) {
        super.setError(errorText)
        errorText?.let {
            val errorTextView = findViewById<TextView>(R.id.textinput_error_text)
            errorTextView.gravity = Gravity.END
        }
    }
}

在您的 XML 上:

<package.TextInputLayoutErrorEnd...>
    <com.google.android.material.textfield.TextInputEditText.../>
</package.TextInputLayoutErrorEnd>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-09-13
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多