19

我有一个小的 EditText,我想在其中显示错误(使用 editText.setError())。在 Android API 10 中,消息显示在很多行中并且不可读。在 Android 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 10 的设备

带有 Android API 15 的平板电脑:

具有 Android API 15 的平板电脑

相关问题。但答案对我不起作用。


更新

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

API 10 API 15

4

5 回答 5

2

因此查看2.3.3 的源代码,错误文本的宽度设置为略小于与其相关的 TextView 的宽度。

他们已经对 4.0.3 进行了改进,因此在您的示例中,弹出窗口的宽度是正确的 - 但您的布局的性质使得指针位于错误的位置。

我认为您有一个针对 4.0.3 的错误报告的合理示例,因为我认为您没有那么不寻常的用例。

为了解决这个问题,我建议您根据需要使用隐藏和显示的 TextView。您可以在编辑文本上设置错误图像,如下所示。

Drawable errorImage = getContext().getResources().getDrawable( R.drawable.your_error_image);
theEditTextInQuestion.setCompoundDrawableWithIntrinsicBounds(errorImage, null, null, null);”
于 2012-05-26T12:38:54.840 回答
1

在 api 14 上,TextView 使用这个类;

private static class ErrorPopup extends PopupWindow {
    private boolean mAbove = false;
    private final TextView mView;
    private int mPopupInlineErrorBackgroundId = 0;
    private int mPopupInlineErrorAboveBackgroundId = 0;

    ErrorPopup(TextView v, int width, int height) {
        super(v, width, height);
        mView = v;
        // Make sure the TextView has a background set as it will be used the first time it is
        // shown and positionned. Initialized with below background, which should have
        // dimensions identical to the above version for this to work (and is more likely).
        mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                com.android.internal.R.styleable.Theme_errorMessageBackground);
        mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
    }

    void fixDirection(boolean above) {
        mAbove = above;

        if (above) {
            mPopupInlineErrorAboveBackgroundId =
                getResourceId(mPopupInlineErrorAboveBackgroundId,
                        com.android.internal.R.styleable.Theme_errorMessageAboveBackground);
        } else {
            mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                    com.android.internal.R.styleable.Theme_errorMessageBackground);
        }

        mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId :
            mPopupInlineErrorBackgroundId);
    }

    private int getResourceId(int currentId, int index) {
        if (currentId == 0) {
            TypedArray styledAttributes = mView.getContext().obtainStyledAttributes(
                    R.styleable.Theme);
            currentId = styledAttributes.getResourceId(index, 0);
            styledAttributes.recycle();
        }
        return currentId;
    }

    @Override
    public void update(int x, int y, int w, int h, boolean force) {
        super.update(x, y, w, h, force);

        boolean above = isAboveAnchor();
        if (above != mAbove) {
            fixDirection(above);
        }
    }
}

像这样打电话;

final float scale = getResources().getDisplayMetrics().density;
            mPopup = new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f));
            mPopup.setFocusable(false);

所以 Android TextView 使用你的手机密度。我尝试更改密度,但我无法工作,因为它需要 root。如果你能做到这一点,也许它的工作。但我想这是不可能的。

于 2012-05-25T16:08:29.507 回答
0

我认为一个问题可能是 XML 中的 LinearLayout 没有 weightSum 属性。如果 LinearLayout 没有声明 weightSum,我不知道您是否可以在子视图中使用 layout_weight。此外,尝试将您的 layout_widths 更改为“wrap_content”,如果这不起作用,请摆脱 layout_weights。

于 2012-08-04T18:39:53.020 回答
0

这两个模拟器的屏幕尺寸是否相同?..如果是这样,我认为设置layout_widths和heights来包装弹出窗口的内容是合适的,如果错误仍然存​​在,那么具体定义弹出窗口的宽度和高度

于 2012-07-29T08:46:34.030 回答
0

I think you should set the width of the edit text as fill parents so that it will take the proper place, otherwise give the width like 200 or 250 dp so that in that particular width your error message will be shown to you.

于 2012-06-15T12:34:58.493 回答