2

我正在使用一种样式TextInputEditText在字段周围放置边框,并TextInputLayout用于设置错误消息。但是当setError ()被调用时,边界消失了,只剩下一个应该有一个字段的空间。

xml:

 <android.support.design.widget.TextInputLayout
                android:id="@+id/input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp">

                 <android.support.design.widget.TextInputEditText
                    android:id="@+id/edittext"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    style="@style/EditTextStyle"
                    android:textColor="@color/secondary_text"
                    android:paddingTop="@dimen/float_hint_margin"
                    android:textSize="15dp" />
            </android.support.design.widget.TextInputLayout>

编辑文本样式:

<style name="EditTextStyle">
        <item name="android:textColor">@color/secondary_text</item>
        <item name="android:background">@drawable/lgedittext</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:textSize">12dp</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_height">30dp</item>
    </style>

lgedit文本:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="@dimen/float_hint_margin">
        <shape>
            <solid android:color="@color/icons" />
            <stroke
                android:width="1dp"
                android:color="@color/divider" />
            <corners android:radius="8dp" />
        </shape>
    </item>
</layer-list>

活动

final TextInputEditText field = (TextInputEditText) dialog.findViewById(R.id.edittext);
final TextInputLayout input = (TextInputLayout) view.findViewById(R.id.input);
String text = field.getText().toString();
   if (!text.equals("")) {
      //do something
   } else {
      input.setError(getString(R.string.error_empty));
   }
4

0 回答 0