gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:21 30 4
gpt4 key购买 nike

我有一个 RelativeLayout 和一个 EditText,它下面有一个 Button。理想情况下,当用户单击 EditText 并填写多行文本时,RelativeLayout 也会随着行的增加而扩展。然而,我现在的行为是,在 EditText 的第一行被填充后,键盘开始覆盖它下面的 Button 并且在 3-4 行文本之后在 EditText 中,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,无法推送`发送:

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 - 软键盘隐藏多行 EditText 下方的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344885/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com