ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – Softkeyboard隐藏多行EditText下方的按钮?

2019-06-28 00:21:49  阅读:245  来源: 互联网

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


我有一个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" />

Comments.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字段中添加更多文本,发送按钮将在键盘下消失,除非我从EditText中删除文本,否则无法推送发送:

Image showing problem

编辑:我还展示了当我点击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);
    }

解决方法:

我试过你的布局一切都很完美,不是问题.

But you need to make changes to your manifest where you declared your activity using more properties to android:configChanges.

Do this inside your android manifest.xml file.

     <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>

我已经测试了上面的变化,它只是工作正常,你可以在下面看到.

enter image description here

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

标签:android,android-layout,android-softkeyboard,android-edittext,relativelayout
来源: https://codeday.me/bug/20190627/1310047.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有