【问题标题】:Default error icon is not displayed for EditText setErrorEditText setError 不显示默认错误图标
【发布时间】:2016-11-06 22:30:54
【问题描述】:

我在我的 Android 应用中对 EditText 进行了验证。错误消息按预期弹出,但未显示默认错误图标。

    if(users.getText().toString().isEmpty()){
        errorNum++;
        users.setError("Field can not be empty");
    } else {
        users.setError(null);
    }

我也尝试了自定义图标,它也不起作用。

users.setError("Field can not be empty",getDrawable(R.drawable.ic_error_outline_black_24dp));

如何获取默认错误图标进行验证

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dinesh.note.MainActivity">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"> 

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:labelFor="@+id/users"
            android:text="@string/users"
android:textAppearance="@android:style/TextAppearance.Widget.TextView" />

        <EditText
            android:id="@+id/users"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
             />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:labelFor="@+id/years"
            android:text="@string/years"


android:textAppearance="@android:style/TextAppearance.Widget.TextView" />

        <EditText
            android:id="@+id/years"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:ems="10"
            android:inputType="number" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:labelFor="@+id/percentage"
            android:text="@string/percentage"
            android:textAppearance="@android:style/TextAppearance.Widget.TextView" />

        <EditText
            android:id="@+id/percentage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:ems="10"
            android:inputType="numberDecimal" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:labelFor="@+id/months"
            android:text="@string/months"
            android:textAppearance="@android:style/TextAppearance.Widget.TextView" />

        <EditText
            android:id="@+id/months"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:ems="10"
            android:inputType="number" />
    </TableRow>
</TableLayout>

<TextView
    android:id="@+id/tamount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/calculate"
    android:layout_marginTop="24dp"
    android:text="@string/tamount"
    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<TextView
    android:id="@+id/amount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tamount"
    android:layout_marginTop="24dp"
    android:text="@string/amount"
    android:textAlignment="center"
    android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
    android:textSize="36sp"
    android:textColor="@color/colorAccent" />

<Button
    android:id="@+id/calculate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:text="@string/calculate"
    android:textColor="@android:color/background_light"
    android:layout_marginTop="227dp"
    android:onClick="calculateBtn"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

样式 xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

【问题讨论】:

  • 你能发布你的布局文件吗?
  • 添加了我的布局文件

标签: java android


【解决方案1】:

我强烈建议您考虑采用现代方法来处理这种事情,原因有一个:随着越来越多的用户升级他们的设备,Google 的向后兼容库已经可以做到这一点。

例如,尝试:TextInputLayout,可通过支持库获得。如果您尚未添加,请在您的 app-level build.gradle 文件中执行此操作。

compile 'com.android.support:design:25.0.0'

然后在你的布局文件中:

<android.support.design.widget.TextInputLayout
    android:id="@+id/quoteIdWrapper"
    android:layout_gravity="start"
    android:layout_width="match_parent"
    android:gravity="start"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/quoteId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:lines="8"
        android:layout_gravity="start"
        android:gravity="top|start"
        android:minLines="5"
        android:maxLines="10"
        android:scrollbars="vertical"
        android:inputType="textCapSentences|textMultiLine"
        android:hint="@string/quote_text"/>

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

然后,您可以从您的代码中获取对这两个项目的引用 (TextInputLayout &amp; EditText)。

接下来,验证 Edittext 字段,如果某些内容无效,请将以下属性设置为 TextInputLayout 引用:

textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Your error message");

这将自动以正确的颜色(通常为红色)向用户发出信号,从而更容易知道有什么事情是遗漏的。

我希望你能明白我的想法。祝你好运!!

【讨论】:

    【解决方案2】:

    有两种方法-

    方法一

    您可以创建自己的EditText 类,方法是重写setError() 方法并使用setCompoundDrawables() 方法显示错误图标。

    public class CustomErrorEditText extends EditText {
    
        public CustomErrorEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setError(CharSequence error, Drawable icon) {
            setCompoundDrawables(null, null, icon, null);
        }
    }
    

    方法二

    setError 中使用drawable 之前,您需要在drawable 上设置边界。

    Drawable drawable = getDrawable(R.drawable.ic_error_outline_black_24dp);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    users.setError("error", drawable);
    

    【讨论】:

    • setError(String) 会给你默认图标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多