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 レベルを除いて、2 つの equals シミュレーターで同じコードを実行しました。結果は画面で確認できます。API 15 ではまだエラーが完全に修正されていません。テキストは判読可能ですが、ポップアップが正しい位置にありません。

API10 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 は携帯電話の密度を使用します。密度の変更を試みましたが、ルートが必要なため機能しませんでした。これができれば、おそらくその仕事です。しかし、それは不可能だと思います。

于 2012-05-25T16:08:29.507 に答える
0

編集テキストの幅を塗りつぶしの親として設定して適切な場所に配置する必要があると思います。そうでない場合は、200または250 dpのような幅を指定して、その特定の幅でエラーメッセージが表示されるようにします。

于 2012-06-15T12:34:58.493 に答える
0

XML の LinearLayout に weightSum 属性がないことが問題である可能性があると思います。LinearLayout に weightSum が宣言されていない場合、子ビューで layout_weight を使用できるかどうかはわかりません。また、layout_widths を「wrap_content」に変更してみて、それが機能しない場合は layout_weights を削除してください。

于 2012-08-04T18:39:53.020 に答える
0

これらの 2 つのエミュレーターの画面サイズは同じですか?

于 2012-07-29T08:46:34.030 に答える