【问题标题】:Small EditText have a setError with a lot of lines小 EditText 有一个带有很多行的 setError
【发布时间】:2012-05-20 04:50:00
【问题描述】:

我有一个小的 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 15 的平板电脑:

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


更新

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

【问题讨论】:

  • 您是否尝试将layout_width 设置为wrap_content
  • 我想到了一个想法 - 您正在使用两个具有不同屏幕尺寸的模拟设备......不是两个运行不同版本平台的匹配模拟设备。所以这可能是两个设备之间像素密度的影响。如果您的模拟器具有相同的屏幕尺寸和密度,是否会以相同的方式出现显示问题?

标签: android android-layout android-edittext


【解决方案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。如果你能做到这一点,也许它的工作。但我想这是不可能的。

【讨论】:

    【解决方案2】:

    所以查看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);”
    

    【讨论】:

      【解决方案3】:

      我认为您应该将编辑文本的宽度设置为填充父项,以便它占据适当的位置,否则将宽度设置为 200 或 250 dp,以便在该特定宽度中向您显示错误消息。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2012-02-25
            • 2018-07-11
            • 2011-05-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多