使用 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");
最佳答案
我真的让它工作了。我必须首先像这样在可绘制对象上设置边界:
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/