【问题标题】:EditText setError with icon not workingEditText setError 图标不起作用
【发布时间】:2015-07-24 10:20:53
【问题描述】:

使用 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 上看到的截图:

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

【问题讨论】:

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


    【解决方案1】:

    我真的让它工作了。我必须首先设置可绘制对象的边界,如下所示:

    Drawable d= getResources().getDrawable(R.drawable.ic_launcher);
                d.setBounds(0, 0, 
                        d.getIntrinsicWidth(), d.getIntrinsicHeight());
    
                et.setError("my error",d);
    

    【讨论】:

    • 谢谢!这做到了。我很惊讶我必须设置内在宽度和高度,即使首选大小也在 image-xml 中设置。有人会认为这不是必需的,但似乎确实如此。做你的建议解决了我的问题(我想它是使用视口宽度和高度而不是内在的)。
    • 对于 api > 21 的人,使用 ContextCompat.getDrawable(context, resId);兼容性问题。
    【解决方案2】:

    Android OS bug with some devices running Jelly Bean/4.2.1 - TextView.setError(CharSequence error) Missing icon

    我发现了这个,它就像魅力一样,希望它也能和你一起工作

    【讨论】:

      猜你喜欢
      • 2016-03-02
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多