我有一个小的 EditText,我想在其中显示错误(使用 editText.setError())。在 Android API 10 中,消息显示在很多行中并且不可读。在 Android 15 中工作得比较好。我在问题的最后附上截图来说明问题。
如何以适当的模式显示错误消息?
我写了一个小例子来重现这个问题:
Activity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((EditText) findViewById(R.id.b)).setError("A error description and bla bla bla bla bla.");
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/a"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/b"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/c"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/d"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/e"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/f"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
采用 Android API 10 的设备:
采用 Android API 15 的平板电脑:
Related question.但答案对我不起作用。
更新
除了 API 级别之外,我在两个 equals 模拟器上执行了相同的代码。结果可以在屏幕上看到。 API 15 仍然没有完全修复错误。文字清晰,但弹出的位置不正确。
最佳答案
所以看看 the source for 2.3.3错误文本的宽度设置为略小于与其相关的 TextView 的宽度。
They've jimmied around with that for 4.0.3因此,在您的示例中,弹出窗口的宽度是正确的 - 但您的布局性质使得指针位于错误的位置。
我认为您有一个针对 4.0.3 的错误报告的合理示例,因为我认为您没有那么不寻常的用例。
为了解决这个问题,我建议您使用一个您在必要时隐藏和显示的 TextView。您可以在编辑文本上设置错误图像,如下所示。
Drawable errorImage = getContext().getResources().getDrawable( R.drawable.your_error_image);
theEditTextInQuestion.setCompoundDrawableWithIntrinsicBounds(errorImage, null, null, null);”
关于android - 小 EditText 有一个带有很多行的 setError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553920/