android - EditText setError 图标不起作用

标签 android android-layout android-edittext android-drawable

使用 Kitkat,但也尝试使用 jellybean,但无法显示自定义错误图标。
我将这两个重载方法用于 setError(...) from Android doc 我只是在做非常简单的代码:

    import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.report_issue);

    RadioGroup rg = (RadioGroup) findViewById(R.id.myradio_group);
    rg.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

    if (checkedId == R.id.radio_two) {
        EditText et = (EditText) findViewById(R.id.et_city);
        // rb.setError("i got a single error");
        et.setError("my error",
                getResources().getDrawable(R.drawable.ic_launcher));

  //as you can see from above im setting a image for the error thru code
  et.requestFocus();
    }
}

这是我的 mylayout.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mycontainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<RadioGroup
    android:id="@+id/myradio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="radio1" />

    <RadioButton
        android:id="@+id/radio_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="radio2" />
</RadioGroup>

<EditText
    android:id="@+id/et_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="enter city" />

</LinearLayout>

这是我在 jellybean 和 kitkat 上看到的截图:

error icon not showing when custom

这是一张图像,当我删除自定义图像图标并仅调用时它工作正常:et.setError("my error"); enter image description here

最佳答案

我真的让它工作了。我必须首先像这样在可绘制对象上设置边界:

Drawable d= getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, 
                    d.getIntrinsicWidth(), d.getIntrinsicHeight());

            et.setError("my error",d);

关于android - EditText setError 图标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24251791/

相关文章:

android - 将小部件添加到我自己的应用程序中。- Android

Android:Edittext::get Entered Character from the keyboard at any position

java - 使用相同 xml 布局文件的多个按钮

android - 将 View 的顶部与 RelativeLayout 中另一个 View 的底部对齐

android - Android 中的绑定(bind)数据

android - 当编辑文本超过二十个时,如何知道我的编辑文本没有改变

java - 如何使用 TextWatcher 检查特定字符串而不是单个字符?

java - Firebase 未触发成功或失败

java - 从 Tumblr 解析 JSONObject

android - 授权的 Twitter 集成问题