【问题标题】:EditText.setError()'s popup message layout looks weirdEditText.setError() 的弹出消息布局看起来很奇怪
【发布时间】:2017-12-26 07:54:04
【问题描述】:

我在工具栏中使用EditText 视图,当单击“检查”图标时,应用程序将检查所有字段是否为空。如果为空,则调用setError() 方法。但是弹出的消息看起来很奇怪(如下图)。

我隐藏了软输入,它看起来像这样。

另一个问题是EditText 视图在根布局中,当调用setError() 时,视图自动向上滚动(Appbarlayout 被隐藏)。如何让布局正确位于视图下方?

这是布局设计的问题吗?

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ActionbarStyle"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <EditText
                android:id="@+id/et_ItemName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/item_name"
                android:inputType="text|textAutoCorrect"
                android:maxLength="20"
                android:textStyle="bold">

                <requestFocus />
            </EditText>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/layout_padding"
        android:paddingLeft="@dimen/layout_padding"
        android:paddingRight="@dimen/layout_padding"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    app:srcCompat="@drawable/ic_money_black_24dp" />

                <EditText
                    android:id="@+id/et_Dollar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:gravity="end|right"
                    android:inputType="numberDecimal"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    app:srcCompat="@drawable/ic_sort_black_24dp" />

                <Button
                    android:id="@+id/btn_SelectCategory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:gravity="end|right|center_vertical"
                    android:hint="@string/select_category" />
            </LinearLayout>

            <EditText
                android:id="@+id/et_Note"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/background_border"
                android:gravity="top|left"
                android:hint="@string/note"
                android:inputType="textMultiLine"
                android:minLines="1"
                android:padding="@dimen/TextPAdding" />

            <include
                android:id="@+id/view_image_container"
                layout="@layout/layout_add_photo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/widget_padding" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 对于测试用例删除&lt;requestFocus /&gt;而不是尝试

标签: android android-layout android-edittext android-toolbar


【解决方案1】:
  • 您可以使用 Snackbar 作为验证,它看起来更好,并且有 定制,因为您可以在特定时期显示适当的消息 time... 示例:在 textChangedListener 或 On Button Click-->

     submitBT = (Button) findViewById(R.id.submitBT);
    
           submitBT.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (loginET.getText().toString().isEmpty() || loginET.length() == 0 || loginET.length() > 50) {
               Snackbar.make(view, "Please Enter Username/Email", Snackbar.LENGTH_SHORT).show();
               return;
           }
           if (passwordET.getText().toString().isEmpty() || passwordET.length() == 0 || passwordET.length() > 50) {
               Snackbar.make(view, "Password", Snackbar.LENGTH_SHORT).show();
           }
               }
           });
    

    你也可以使用 textChangedListener... 并且视图不会像 seterror 那样自动向上滚动

【讨论】:

  • 这不能回答实际问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2018-09-12
  • 2021-03-25
  • 2015-03-20
  • 2011-07-19
  • 2021-12-03
相关资源
最近更新 更多