我有一个小的edittext,我想在其中显示错误(使用edittext.setError())。在android api 10中,消息显示在很多行中,并且不可读。在安卓系统中,15的运行相对良好。我附上截图来说明问题结束时的问题。
如何以适当的模式显示错误消息?
我写了一个小例子来重现这个问题:
活动:

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的平板电脑:
但答案对我不起作用。
更新
我在两个Equals模拟器上执行了相同的代码,API级别除外。结果可以在屏幕上看到。API 15仍然不能完全修复错误。文本清晰可见,但弹出窗口位置不正确。

最佳答案

因此,查看the source for 2.3.3时,错误文本的宽度设置为略小于与之相关的textView的宽度。
They've jimmied around with that for 4.0.3这样,在您的示例中,弹出窗口的宽度是正确的-但是布局的性质是指针位于错误的位置。
我认为你有一个针对4.0.3的bug报告的合理例子,因为我认为你没有那么不寻常的用例。
为了解决这个问题,我建议您使用一个您根据需要隐藏和显示的文本视图。您可以在编辑文本上设置错误图像,如下所示。

Drawable errorImage = getContext().getResources().getDrawable( R.drawable.your_error_image);
theEditTextInQuestion.setCompoundDrawableWithIntrinsicBounds(errorImage, null, null, null);”

08-04 09:12
查看更多