【问题标题】:Android - How to remove exclamation mark of setError()Android - 如何删除 setError() 的感叹号
【发布时间】:2017-03-02 09:49:46
【问题描述】:

我的 xml 代码的密码字段部分:

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
...

    <android.support.design.widget.TextInputLayout
        android:layout_below="@+id/uname_ly"
        android:id="@+id/text_input_layout_passwd"
        app:layout_widthPercent="70%"
        android:layout_centerHorizontal="true"
        app:layout_heightPercent="10%"
        app:layout_marginTopPercent="0%"
        app:layout_marginBottomPercent="0%"
        android:adjustViewBounds="true"
        android:textColorHint="@color/editTextHintColor"
        app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
        >

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/nopasswd"
            android:inputType="textPassword"
            android:maxLines="1"
            android:textColor="@color/editTextTextColor" />

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

如何在调用 setError() 时删除这个覆盖的红色感叹号?

[更新样式]

<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/editTextHintColor</item>
</style>

【问题讨论】:

  • 看看这个对你有帮助stackoverflow.com/q/5218691/5773037
  • TextInputLayout 上调用setError(),而不是EditText
  • @MikeM。我已经尝试过TextInputLayout mPasswordL = (TextInputLayout) rootView.findViewById(R.id.text_input_layout_passwd); mPasswordLsetError(Html.fromHtml("&lt;font color='white'&gt;" + getString(R.string.error_empty_password) + "&lt;/font&gt;"));,但它不起作用。
  • @MikeM。抱歉,它确实有效,但它删除了整个框,只留下红线+没有文字。

标签: android android-layout android-edittext android-styles android-textinputlayout


【解决方案1】:

如果您使用 TextInputLayout,我认为这是一个更优化的答案。 我所做的就是利用这个错误,我只是在 2 秒后将文本输入布局恢复到默认状态。

otpIn.setError(obj.getString("status"));

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
otpIn.setError(null);
}
},2000);

【讨论】:

    【解决方案2】:

    TextInputLayout 现在是新 ma​​terial 包的一部分。以下是关于我如何通过验证获得密码字段的更新:

    布局:

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/loginPasswordInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/grid"
        android:hint="@string/onboarding_hint_password"
        app:errorEnabled="true"
        app:passwordToggleContentDescription="@string/onboarding_toggle_description_password"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="?colorAccent">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/loginPasswordInputText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|center"
            android:inputType="textPassword"
            android:lines="1"
            tools:text="@string/onboarding_hint_password" />
    </com.google.android.material.textfield.TextInputLayout>
    

    然后可以通过以下方式设置错误消息并隐藏代码中的图标:

    loginPasswordInputLayout.error = errorMessage
    // Error icon overlaps with password visibility toggle, so remove it:
    loginPasswordInputLayout.setErrorIconDrawable(0)
    

    这样您可以获得红色(或您选择的颜色)文本和下划线,但密码可见性切换图标仍可完全访问。

    【讨论】:

      【解决方案3】:

      使用setError(CharSequence error, Drawable icon) 方法。将drawable设置为null:

      editText.setError("Pls provide email", null);
      

      【讨论】:

      • 谢谢你救了我的命。
      • 此方法现在不起作用 - 2021 年 7 月
      猜你喜欢
      • 2010-12-10
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2015-09-01
      • 2021-01-03
      • 1970-01-01
      相关资源
      最近更新 更多