安卓编辑框错误覆盖了其他组件

3
我有一个问题,就是编辑文本显示的错误消息会覆盖布局中的其他组件。
有没有人知道如何将错误消息显示在顶部和底部布局之后?
简单解释下结构: 顶部红色和底部灰色部分都是线性布局。 主要内容部分是Frame Layout,并由应用程序处理。在这种情况下,有几个编辑文本被包含在ScrollView中。
<android.support.v4.widget.CustomDrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:fitsSystemWindows="true">

    <com.custom.TitlebarLayout
        android:id="@+id/titlebarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"/>


    <FrameLayout
        android:id="@+id/contentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_gravity="top"/>

    <com.custom.ButtonsToolbarLayout
        android:id="@+id/buttonsToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical|left"
        android:orientation="horizontal"/>

</RelativeLayout>

contentContainer 内部的布局如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         xmlns:msa="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".fragment.addressedit.AddressEditFragment"
         android:id="@+id/frame_layout_address_edit">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/scrollview_address_dialog"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:orientation="horizontal">

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearlayout_address_top"
            android:hint="Name"
            android:maxLength="30"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textCapCharacters"
            android:tag="etx_name1"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_name2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_name"
            android:hint="Name2"
            android:maxLength="30"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textCapCharacters"
            android:tag="etx_name2"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_street"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_name2"
            android:hint="Street"
            android:maxLength="30"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textCapCharacters"
            android:tag="etx_street"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_houseno"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_street"
            android:hint="House No."
            android:maxLength="8"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="number"
            msa:alternativeInputType="textNoSuggestions"
            android:tag="etx_house_no"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_postcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_houseno"
            android:hint="Postcode"
            android:maxLength="10"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textPostalAddress"
            android:tag="etx_postcode1"
            msa:validation="postcode"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_postcode"
            android:hint="City"
            android:maxLength="30"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textCapCharacters"
            android:tag="etx_city"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_country"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_city"
            android:hint="Country"
            android:maxLength="5"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="textCapCharacters"
            android:tag="etx_country"/>

        <com.custom.editview.EditViewLayout
            android:id="@+id/edittext_address_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edittext_address_country"
            android:hint="Phone"
            android:maxLength="20"
            android:maxLines="1"
            android:singleLine="true"
            android:inputType="phone"
            android:tag="etx_phone1"/>

    </RelativeLayout>

</ScrollView>


1
请您在描述中分享代码。 - Rahul
什么是顶部和底部布局的后面?这是否意味着在编辑文本的末尾?如果是这样,您可以在编辑文本布局的末尾实现自定义文本视图。您可以使用固定大小的相对布局或甚至使用带权重的水平线性布局来分隔您的编辑文本和文本视图。 - HendraWD
@HendraWijayaDjiono 在附加的截图中,您可以看到第二张图片上方显示了错误消息。我期望错误消息将显示在红色部分后面(因此在第二张图片上不会出现任何错误消息)。 - swiki
3个回答

2
只需进行简单的调整,您就可以开始了... 最初的回答。
scrollView.setOnTouchListener(new View.OnTouchListener() {
 @Override
        public boolean onTouch(View v, MotionEvent event) {
            addressName.clearFocus();
            addressName2.clearFocus();
            houseNo.clearFocus();
            phone.clearFocus();
            return false;
        }
    })

;


0
将错误信息传递给TextInputLayout。
...
    txtInputLayout.setErrorEnabled();
    txtInputLayout.setError("error text");
...

错误将显示在EditText下方。


我不想在编辑文本下显示它,我想在顶部红色和底部灰色布局后面显示它。 - swiki

0

有几种方法可以解决你的问题。

  1. 使用Toast

    Toast.makeText(getApplicationContext(), validate_msz, Toast.LENGTH_SHORT).show();
    
  2. 使用RelativeLayout将您的错误消息布局设置为底部和顶部

    <RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin"
      >
     <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="0dp">
    
    <Button
        android:id="@+id/btn_password_save"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:background="#e91043"
        android:padding="10dp"
        android:paddingBottom="0dp"
        android:text="保存"
        android:textColor="@android:color/white" />
    
      </LinearLayout>
       </RelativeLayout>
    
  3. 根据需要使用浮动操作按钮。

    http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html

  4. 自定义对话框。


@swiki,在相对布局中的LinearLayout中只需使用以下属性android:layout_alignParentBottom="true"和android:gravity="bottom"。 - Laxmeena
也许我在问题中用错了单词。我的意思是在顶部和底部布局后面显示,而不是前面 :) 当向上/向下滚动时,我希望将其放在后面显示,而不是在这些布局的前面。 - swiki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接