首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >小EditText有一个setError,有很多行

小EditText有一个setError,有很多行
EN

Stack Overflow用户
提问于 2012-05-11 23:02:27
回答 5查看 4K关注 0票数 19

我有一个很小的EditText,我想在其中显示错误(使用editText.setError())。在Android API10中,消息显示在很多行中,并且无法阅读。在Android 15中,运行得相对较好。我在问题的末尾附上屏幕截图来说明问题。

如何以适当的模式显示错误消息?

我写了一个小例子来重现这个问题:

活动:

代码语言:javascript
复制
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.");
}

布局:

代码语言:javascript
复制
<?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仍然不能完全修复错误。文本清晰可读,但弹出窗口的位置不正确。

EN

回答 5

Stack Overflow用户

发布于 2012-05-26 20:38:55

因此,查看the source for 2.3.3,错误文本的宽度被设置为略小于与之相关的TextView的宽度。

They've jimmied around with that for 4.0.3,因此,在您的示例中,弹出窗口的宽度是正确的-但布局的本质是指针位于错误的位置。

我认为你有一个关于4.0.3的bug报告的合理例子,因为我不认为你有一个不寻常的用例。

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

代码语言:javascript
复制
Drawable errorImage = getContext().getResources().getDrawable( R.drawable.your_error_image);
theEditTextInQuestion.setCompoundDrawableWithIntrinsicBounds(errorImage, null, null, null);”
票数 2
EN

Stack Overflow用户

发布于 2012-05-26 00:08:30

在API14上,TextView使用这个类;

代码语言:javascript
复制
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);
        }
    }
}

像这样打电话;

代码语言:javascript
复制
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使用你的手机密度。我试着改变密度,但我不能工作,因为它需要根。如果你能做到这一点,也许它是有效的。但我想这是不可能的。

票数 1
EN

Stack Overflow用户

发布于 2012-06-15 20:34:58

我认为您应该将编辑文本的宽度设置为fill parents,以便将其放在适当的位置,否则,给出宽度,如200或250 dp,以便在该特定宽度下,您的错误消息将显示给您。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10553920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档