软键盘隐藏多行EditText下方的按钮?

时间:2016-09-06 09:03:21

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

我的RelativeLayout EditText低于Button。理想情况下,当用户点击EditText并填写多行文字时,RelativeLayout也会随着行的增加而扩展。但是,我现在的行为是,在填充EditText的第一行后,键盘开始覆盖Button下面的EditText以及Button中的3-4行文本}, <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" /> 已经低于键盘。是否有任何方法可以让我在键入文本时,整个布局的大小增加,以便按钮保持在键盘上方?这是我的代码:

的AndroidManifest.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>

Comments.xml:

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> 创建一个滚动容器,这样可以帮助扩展布局,但这似乎不起作用。任何帮助,将不胜感激。谢谢!

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

EditText

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

Image showing problem

编辑:如果有帮助的话,我在此处点击EditText时,也会向我展示如何滚动到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); } 的底部。

get file="C:\Program Files\IBM\SPSS\Statistics\23\Samples\English\Employee data.sav".
begin program.
import spss, spssaux, spssdata
spss.Submit("set mprint on.")
vd=spssaux.VariableDict(variableType ="numeric")
for v in vd:
    allvalues = list(set(item[0] for item in spssdata.Spssdata(v.VariableName, names=False).fetchall()))
    if allvalues:
        cmd="value labels " + v.VariableName + "\n".join(["  %(i)s '%(i)s'" %locals() for i in allvalues if i <> None]) + "."
        spss.Submit(cmd)
spss.Submit("set mprint off.")
end program.

4 个答案:

答案 0 :(得分:3)

我试过你的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.

enter image description here

希望这可以帮助您解决问题。

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

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

<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

答案 3 :(得分:0)

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

希望这有帮助。