2016-11-05 79 views
0

我有我的Android应用程序中的EditText验证。错误消息按预期弹出,但不显示默认错误图标。默认错误图标不显示EditText setError

enter image description here

​​

我也试图与自定义图标,它不工作过。

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> 
+1

你能发布你的布局文件吗? –

+0

添加了我的布局文件 – Dinesh

回答

1

我会认真建议你考虑现代的方法来这种事情的原因之一是:更多的用户升级他们的设备,从谷歌向后兼容库已经可以做到这一点。

例如,尝试: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 & EditText)引用。

接着,验证EDITTEXT字段,如果某物是无效的,设置以下属性到TextInputLayout参考:

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

这将自动与正确的彩色信号(红色常常)给用户使其更容易知道有什么是一个小姐。

我希望你明白我的意思。祝你好运!!

+1

谢谢你的方法..它的工作.. :) – Dinesh

1

有两种ways-

方法1

您可以通过重写setError()方法来创建你自己的类的EditText和使用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); 
    } 
} 

方法2

您需要setError使用前设置的绘制边界。

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

有没有办法获得默认错误图标? – Dinesh

+0

'setError(String)'会给你默认图标 –

相关问题