Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 小EditText有一个setError,有很多行_Android_Android Layout_Android Edittext - Fatal编程技术网

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

Android 小EditText有一个setError,有很多行,android,android-layout,android-edittext,Android,Android Layout,Android Edittext,我有一个小的EditText,我想在其中显示错误(使用EditText.setError())。在Android API 10中,消息以很多行显示,无法读取。在安卓系统中,15工作得相对较好。我在问题的末尾附上截图来说明问题 如何以适当的模式显示错误消息 我写了一个小例子来重现这个问题: 活动: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCont

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

但这个答案对我来说并不适用


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


在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使用你的手机密度。我试图改变密度,但我无法工作,因为它需要根。如果你能做到这一点,也许它的工作。但是我想这是不可能的。

因此查看错误文本的宽度设置为略小于与其相关的文本视图的宽度

因此,在您的示例中,弹出窗口的宽度是正确的,但布局的性质是指针位于错误的位置

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

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

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

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

这两个模拟器的屏幕大小是否相同?。。如果是这样,我认为应该为弹出窗口设置布局宽度和包装内容的高度,如果错误仍然存在,那么具体定义弹出窗口的宽度和高度。我认为问题可能在于XML中的LinearLayout没有weightSum属性。如果LinearLayout没有声明权重和,我不知道是否可以在子视图中使用布局权重。另外,尝试将版面宽度更改为“包裹内容”,如果不起作用,则取消版面权重。

您是否尝试将
版面宽度设置为
包裹内容
?我想到了一个想法-您正在使用两个屏幕大小不同的模拟设备。。。没有两个匹配的仿真设备运行不同版本的平台。因此,这可能是两个设备之间像素密度的影响。如果您的模拟器具有相同的屏幕大小和密度,是否会以相同的方式出现显示问题?