【问题标题】:Customizing the setError icon for EditText自定义 EditText 的 setError 图标
【发布时间】:2012-12-04 16:23:15
【问题描述】:

我想为我的EditText 设置一个setError 方法,使用自定义图标而不是默认的Android 图标。所以我尝试了这个:

((EditText)findViewById(R.id.edtTitle)).setError(getResources().getText(R.string.errEmptyTitle), 
         getResources().getDrawable(R.drawable.ico_warning_small);

它向我显示自定义消息,但不显示自定义图标。我也试过这个:

Drawable warning = (Drawable)getResources().getDrawable(R.drawable.ico_warning_small);
((EditText)findViewById(R.id.edtTitle))
      .setError(getResources().getText(R.string.errEmptyTitle), warning);

差不多,但我还是决定试一试。然而,这也没有帮助 - 我仍然看不到图标。 我尝试使用其他一些 Android 系统图标,只是想看看我是否看到它们,不,我也没有看到它们。

那么我做错了什么?有没有办法设置那个自定义图标?

【问题讨论】:

标签: android icons textview


【解决方案1】:

在setError中使用drawable之前,你需要设置它的边界。

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
editText.setError("error", drawable);

【讨论】:

  • 这解决了我的问题。谢谢。
  • 谢谢 - 花了一个小时!
  • 使用画布和绘画制作新的可绘制对象,getIntrinsicWidth 和 getIntrinsicHeight 返回 -1,有什么线索吗?
【解决方案2】:

如果您根本不想显示任何图标,请使用

editText.setError("error", null);

【讨论】:

    【解决方案3】:

    这里讨论并解决了这个问题:

    EditText setError() with icon but without Popup message

    我希望通过扩展答案不会自动转换为评论。

    【讨论】:

      【解决方案4】:
          Drawable customErrorDrawable = getResources().getDrawable(R.drawable.error_icon);
              customErrorDrawable.setBounds(0, 0, customErrorDrawable.getIntrinsicWidth(), customErrorDrawable.getIntrinsicHeight());
      
      
       editText.setError("please enter data",customErrorDrawable);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多