【问题标题】:Softkeyboard hides button below multi-line EditText?软键盘隐藏多行EditText下方的按钮?
【发布时间】:2017-01-13 16:02:01
【问题描述】:

我有一个RelativeLayout 和一个EditText,下面有一个Button。理想情况下,当用户单击EditText 并填写多行文本时,RelativeLayout 也会随着行数的增加而扩展。但是,我现在的行为是,在EditText 的第一行被填满后,键盘开始覆盖它下面的Button,在EditText 中的3-4 行文本之后,Button消失在键盘下方。有什么办法可以让我在输入文本时,整个布局的大小会增加,以便按钮保持在键盘上方?这是我的代码:

AndroidManifest.xml:

    <activity
        android:name="<Name of project>.activity.Comments"
        android:configChanges="orientation|keyboardHidden"
        android:label="Comments"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden|adjustResize" />

评论.xml:

<RelativeLayout
        android:id="@+id/send_message"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_margin="4dp"
        android:layout_alignParentBottom="true">

        <EditText
            android:fontFamily="sans-serif"
            android:id="@+id/write_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:gravity="left"
            android:textSize="16sp"
            android:textColor="@color/black"
            android:cursorVisible="false"
            android:textColorHint="@color/material_color_grey_300"
            android:hint="@string/commentBack"
            android:background="@drawable/edittext_bg"
            android:inputType="textMultiLine"
            android:isScrollContainer="true"
            android:maxLength="200"
            android:scrollHorizontally="false" />

        <Button
            android:fontFamily="sans-serif-medium"
            android:id="@+id/send_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="8dp"
            android:text="@string/send"
            android:background="@null"
            android:layout_below="@id/write_comment"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:textAllCaps="false" />
    </RelativeLayout>

我尝试添加android:isScrollContainer="true",认为它会为EditText 创建一个滚动容器,以便帮助扩展布局,但这似乎不起作用。任何帮助,将不胜感激。谢谢!

编辑:根据以下答案在整个布局中添加:

<?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:fitsSystemWindows="true"
    android:id="@+id/comments_layout">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/comments_appbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_alignParentTop="true"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:id="@+id/comments_listview"
            android:layout_height="match_parent">

            <ListView
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rv_view_comments">
            </ListView>

            <TextView
                android:layout_below="@id/rv_view_comments"
                android:gravity="center_horizontal"
                android:padding="16dp"
                android:textSize="16sp"
                android:id="@+id/empty_list_item"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="gone"
                android:text="@string/noComments" >
            </TextView>

            <include layout="@layout/include_progress_overlay"/>

        </RelativeLayout>

        <RelativeLayout
    android:id="@+id/send_message"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_margin="4dp"
    android:layout_alignParentBottom="true">

    <EditText
        android:fontFamily="sans-serif"
        android:id="@+id/write_comment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:gravity="left"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:cursorVisible="false"
        android:textColorHint="@color/material_color_grey_300"
        android:hint="@string/commentBack"
        android:background="@drawable/edittext_bg"
        android:inputType="textMultiLine"
        android:isScrollContainer="true"
        android:maxLength="200"
        android:scrollHorizontally="false" />

    <Button
        android:fontFamily="sans-serif-medium"
        android:id="@+id/send_comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/colorPrimary"
        android:layout_marginTop="8dp"
        android:text="@string/send"
        android:background="@null"
        android:layout_below="@id/write_comment"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:textAllCaps="false" />
</RelativeLayout>

    </RelativeLayout>

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

编辑:根据 Jaydroider 的要求,我附上了一张照片来展示我的问题。如果我可以制作 GIF 或其他内容,如果我在 EditText 字段中添加更多文本,Send 按钮将在键盘下方消失,除非我从 EditText 中删除文本,否则无法推送“发送:

编辑:如果有帮助,我还展示了当我在此处单击 EditText 时如何滚动到 ListView 的底部。

public void setupComment(final EditText comment) {
        //Once we send the post, we want to scroll to bottom of listview
        comment.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    comment.setImeOptions(EditorInfo.IME_ACTION_DONE);
                    resetKeyboardSettings();
                }
                return false;
            }
        });

        //Hide the cursor until view is clicked on
        View.OnTouchListener onTouchListener = new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("Touched");
                if (v.getId() == comment.getId()) {
                    comment.setCursorVisible(true);
                }
                commentsView.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        commentsView.smoothScrollToPosition(commentsView.getAdapter().getCount());
                    }
                }, 750);
                return false;
            }
        };
        comment.setOnTouchListener(onTouchListener);
    }

【问题讨论】:

  • 当我检查了你的布局后,我只能看到List View 没有任何其他小部件。请详细说明。
  • @jaydroider 我已经更新了上面的代码来演示我的问题。希望这会有所帮助。它在Edit: Added in entire layout based on answer below:的编辑下
  • 是的,我已经检查了我这边的布局,我需要澄清edit textbutton 的显示位置。目前我只能看到List View
  • 你能提供一个你的设计的截图吗?
  • @jaydroider 我提供了一个显示我的设计的屏幕显示。希望对您有所帮助。

标签: android android-layout android-edittext android-relativelayout android-softkeyboard


【解决方案1】:

我已经用你的Layout 试过了,一切都很完美,这不是问题。

But you need to make changes to your manifest where you declared your activity 使用更多属性到android:configChanges

在您的 android manifest.xml 文件中执行此操作。

     <activity
            android:name="<Name of project>.activity.Comments"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="Comments"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize"/>

这是我使用的 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:id="@+id/comments_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.PopupOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/comments_appbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/comments_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/send_message"
            android:layout_alignParentTop="true"
            android:layout_marginTop="?attr/actionBarSize">

            <ListView
                android:id="@+id/rv_view_comments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/empty_list_item"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <TextView
                android:id="@+id/empty_list_item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rv_view_comments"
                android:gravity="center_horizontal"
                android:padding="16dp"
                android:text="No Comments"
                android:textSize="16sp"
                android:visibility="gone" />


        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_margin="4dp">

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:cursorVisible="false"
                android:fontFamily="sans-serif"
                android:gravity="left"
                android:hint="Comment Back"
                android:inputType="textMultiLine"
                android:isScrollContainer="true"
                android:maxLength="200"
                android:padding="12dp"
                android:scrollHorizontally="false"
                android:textColor="#000000"
                android:textColorHint="@android:color/holo_red_dark"
                android:textSize="16sp" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@id/write_comment"
                android:layout_marginTop="8dp"
                android:background="@null"
                android:fontFamily="sans-serif-medium"
                android:gravity="center|center_vertical"
                android:text="Send"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimary"
                android:textSize="16sp" />
        </RelativeLayout>

    </RelativeLayout>

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

I have tested with above changes and it's just working fine you can see below.

希望这能帮助您解决问题。

【讨论】:

  • 嘿,您介意发布您的代码以便我可以模仿吗?我试图听听您对 Manifest 文件的更改,但这似乎并没有为我解决问题。
  • 您用于创建上述图像的 XML 布局代码?
  • 嘿,我实际上尝试了您的代码,它似乎对我有用。感谢你的帮助!我想我必须在按钮重力上解决一些问题,还必须制作RelativeLayoutwrap_content。谢谢!
  • @user1871869 干杯。也支持答案,以便将来对其他人有所帮助。
【解决方案2】:

我的解决方案是在布局中添加ScrollView 作为根视图并覆盖视图的onSizeChanged 方法。如果EditText 的高度增加并且键盘挡住了您的按钮,则Smooth Scroll 到您的按钮。

希望这会有所帮助。

【讨论】:

  • 我怎么会有一个 ScrollView 和一个嵌套的 ListView 呢?我明白你在说什么,但我觉得这对我来说行不通。
【解决方案3】:

将所有编辑文本和按钮放在滚动视图中。

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
               <Edittext
               <EditText
               <EditText
               <Btn
             </LinearLayout
        </scrollview

【讨论】:

    【解决方案4】:

    试试这个:

    <RelativeLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_alignParentBottom="true">
    
            <EditText
                android:fontFamily="sans-serif"
                android:id="@+id/write_comment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dp"
                android:gravity="left"
                android:textSize="16sp"
                android:textColor="@color/black"
                android:cursorVisible="false"
                android:textColorHint="@color/material_color_grey_300"
                android:hint="@string/commentBack"
                android:background="@drawable/edittext_bg"
                android:inputType="textMultiLine"
                android:isScrollContainer="true"
                android:maxLength="200"
                android:scrollHorizontally="false" />
    
            <Button
                android:fontFamily="sans-serif-medium"
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:textColor="@color/colorPrimary"
                android:layout_marginTop="8dp"
                android:text="@string/send"
                android:background="@null"
                android:layout_below="@id/write_comment"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:textAllCaps="false" />
        </RelativeLayout>
    

    【讨论】:

    • 嘿,我试着按照你的代码。好像您刚刚在RelativeLayout 标签中添加了android:layout_height = wrap_content。我已尝试实施您的建议,但这似乎不起作用,现在ListView 我已经涵盖了我的EditText 我在上面发布了我的整个布局,希望它会更清楚并可以帮助我解决这个问题。我试过你上面提到的,但因为EditText被隐藏了,我不知道如何修复它。
    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2012-05-31
    • 2017-01-31
    相关资源
    最近更新 更多