gpt4 book ai didi

android - 基于键盘的 EditText 高度调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:50 27 4
gpt4 key购买 nike

我在 android 中有一个 Activity ,其中有一个编辑框和编辑框下方的 3 个按钮。请查看附件。启动此 Activity 时,默认状态为 STATE1。(请参见图片)。默认情况下键盘是可见的。现在,当我按下后退按钮或放置键盘时,我希望调整编辑文本的大小并占据整个屏幕,如 STATE2 所示。

我不确定如何完成此操作。我根据目标设备将 edittext 的高度硬编码为某个 dp。我相信这必须改变。任何人都可以帮助我如何实现这一目标。

布局文件的XML以及屏幕截图如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/EditMessage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="@drawable/newback"
android:gravity="top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
android:maxLength="200"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:id="@+id/PostMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_marginRight="0.2dp"
android:background="@drawable/newbutton_corner"
android:text="@string/SubmitMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<Button
android:id="@+id/CancelMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_marginRight="0.2dp"
android:background="@drawable/newbutton_corner"
android:text="@string/CancelMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<Button
android:id="@+id/DeleteMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@drawable/newbutton_corner"
android:text="@string/DeleteMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

enter image description here

最佳答案

如下更改您的 EditText:

<EditText
android:id="@+id/EditMessage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
android:maxLength="200"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
/>

添加layout_weight = 1

在我看来,您不需要需要adjustPan。我会留给你来决定。行为会有所不同,您可以尝试一下。以下是文档中关于 adjustPan 的内容:

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

如果使用它,您底部的按钮可能会被隐藏。 改为使用 adjustResize:将这一行放在 AndroidManifest

的 Activity 里面
android:windowSoftInputMode="stateVisible|adjustResize"

关于android - 基于键盘的 EditText 高度调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20397138/

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