android - 小 EditText 有一个带有很多行的 setError

标签 android android-layout android-edittext

我有一个小的 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 的设备:

Device with Android API 10

采用 Android API 15 的平板电脑:

Tablet with Android API 15

Related question.但答案对我不起作用。


更新

除了 API 级别之外,我在两个 equals 模拟器上执行了相同的代码。结果可以在屏幕上看到。 API 15 仍然没有完全修复错误。文字清晰,但弹出的位置不正确。

API 10 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/

相关文章:

Android:如何使 AlertDialog 在单击确定按钮时消失?

android - 调用函数删除一行

android - 弹出窗口的宽度和高度

java - 如果 editText 初始化为仅接受浮点值,如何防止添加 "."?

java - 如何从声明 edittext 的另一个类中的线程更改 edittext 的文本?

java - 使用 Spans 获取 EditText 的左侧部分文本

java - ThreadPoolExecutor 的 maximumPoolSize 是如何工作的?

java - 如何使用下载的 android holo 图标、模具、字体?

android - 列表项背景上的进度条

安卓。文本、图像和文本。使用什么 Android 小部件?