adjustPan 不阻止键盘覆盖 EditText

发布于 2024-10-09 19:34:33 字数 1536 浏览 3 评论 0原文

我正在尝试创建一个非常基本的聊天屏幕,其中包含显示文本的 ListView 和底部的 EditText,以及 EditText 右侧的“发送”按钮。一切功能正常,但是当我单击 EditText 时,虚拟键盘会覆盖它。屏幕稍微平移,但不足以在键盘上方可见。我的清单中有“adjustPan”标签,并且也尝试了“adjustResize”标签但无济于事。我猜这与我的布局设置方式有关,但老实说我不知道​​。请帮忙!

当前布局...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@+id/android:list" 
    android:layout_height="0dip" 
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:stackFromBottom="true">
</ListView>

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

    <EditText android:id="@+id/sendMessageBox"
        android:focusable="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical"
        android:maxLines="4"
        android:text=""
        android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
        android:maxLength="1000"
        android:hint="Type your message..."
        android:imeOptions="actionSend"/>

    <Button android:id="@+id/sendMessageButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Send"/>

</LinearLayout>

I'm trying to create a pretty basic chat screen with a ListView displaying the text and an EditText at the bottom and a "Send" button to the right of the EditText. Everything is functional, but when I click the EditText, the virtual keyboard covers it. The screen pans up a little but not enough to become visible above the keyboard. I've got the "adjustPan" tag in my manifest and have also tried the "adjustResize" tag to no avail. I'm guessing it has something to do with the way my layout is set up, but I honestly have no clue. Please help!

Current Layout...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@+id/android:list" 
    android:layout_height="0dip" 
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:stackFromBottom="true">
</ListView>

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

    <EditText android:id="@+id/sendMessageBox"
        android:focusable="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical"
        android:maxLines="4"
        android:text=""
        android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
        android:maxLength="1000"
        android:hint="Type your message..."
        android:imeOptions="actionSend"/>

    <Button android:id="@+id/sendMessageButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Send"/>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

哽咽笑 2024-10-16 19:34:33

经过大量搜索后,显然这就是我所说的错误。如果您使用全屏标签(从活动中删除状态栏),则在不将活动包装在 ScrollView 中的情况下无法使用“调整调整大小”。不幸的是,对我来说,我使用的是 ListView,这会产生另一个问题。我厌倦了搞乱它,可能会放弃该活动的全屏。

After doing a lot of searching apparently it's what I'm calling a bug. If you use the fullscreen tag (to remove the status bar from the activity) you can't use "adjustResize" without wrapping the activity in a ScrollView. Unfortunately for me I'm using a ListView which would create yet another problem. I'm sick of messing with it and will probably just abandon the fullscreen on that activity.

假情假意假温柔 2024-10-16 19:34:33

在清单文件中,您需要设置适当的 android:windowSoftInputMode 属性。此属性自 API 3 起有效。

<activity
    ...
    android:windowSoftInputMode="adjustPan" >
</activity>

选项为: http:// /developer.android.com/guide/topics/manifest/activity-element.html#wsoft

  • stateUnspecified 未指定软键盘的状态(无论是隐藏还是可见)。系统会选择一个
    适当的状态或依赖于主题中的设置。 这是
    软键盘行为的默认设置。
  • stateUnchanged 当 Activity 到达时,软键盘将保持其上次所处的状态,无论是可见还是隐藏。
    前面。
  • "stateHidden" 当用户选择活动时(即当用户确定向前导航时),软键盘会隐藏
    回到原来的活动,而不是因为离开另一个活动而退回去
    活动。
  • stateAlwaysHidden 当 Activity 的主窗口具有输入焦点时,软键盘始终隐藏。
  • stateVisible 软键盘在通常适当的情况下可见(当用户向前导航到 Activity 的
    主窗口)。
  • stateAlwaysVisible 当用户选择该活动时(即,当用户肯定地选择该活动时),软键盘变得可见
    向前导航至活动,而不是后退,因为
    离开另一项活动。
  • 调整未指定 未指定 Activity 的主窗口是否调整大小以便为软键盘腾出空间,或者是否调整 Activity 的主窗口大小。
    平移窗口内容以使当前焦点可见
    屏幕上。系统会自动选择其中一种模式
    取决于窗口的内容是否有任何布局视图
    可以滚动其内容。如果有这样的风景,窗外
    将调整大小,假设滚动可以使所有
    窗口的内容在较小的区域内可见。这是默认设置
    主窗口行为的设置。
  • 调整调整大小 Activity 的主窗口始终会调整大小,以便为屏幕上的软键盘腾出空间。
  • adjustPan 活动的主窗口不会调整大小以为软键盘腾出空间。相反,窗口的内容是
    自动平移,以便当前焦点不会被遮挡
    键盘和用户始终可以看到他们正在输入的内容。这是
    通常不如调整大小那么理想,因为用户可能需要
    关闭软键盘即可访问被遮挡的部分并与之交互
    窗户。

In your manifest file, you need to set the appropriate android:windowSoftInputMode property. This attribute is valid since API 3.

<activity
    ...
    android:windowSoftInputMode="adjustPan" >
</activity>

Options are: http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

  • stateUnspecified The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an
    appropriate state or rely on the setting in the theme. This is the
    default setting for the behavior of the soft keyboard.
  • stateUnchanged The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the
    fore.
  • "stateHidden" The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward
    to the activity, rather than backs into it because of leaving another
    activity.
  • stateAlwaysHidden The soft keyboard is always hidden when the activity's main window has input focus.
  • stateVisible The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's
    main window).
  • stateAlwaysVisible The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively
    navigates forward to the activity, rather than backs into it because
    of leaving another activity.
  • adjustUnspecified It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the
    contents of the window pan to make the current focus visible
    on-screen. The system will automatically select one of these modes
    depending on whether the content of the window has any layout views
    that can scroll their contents. If there is such a view, the window
    will be resized, on the assumption that scrolling can make all of the
    window's contents visible within a smaller area. This is the default
    setting for the behavior of the main window.
  • adjustResize The activity's main window is always resized to make room for the soft keyboard on screen.
  • 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.
離殇 2024-10-16 19:34:33

如果您在清单中为 Activity 设置了 android:windowSoftInputMode="adjustResize",则 ScrollView(或其他可折叠 ViewGroup)将缩小以容纳软键盘。但是,如果您在 Activity 的主题中设置 android:windowFullscreen="true" ,则 ScrollView 不会缩小,因为它被迫填满整个屏幕。但是,在主题中设置 android:fitsSystemWindows="false" 也会导致 adjustmentResize 不起作用

if you set android:windowSoftInputMode="adjustResize" for an activity in the manifest, then a ScrollView (or other collapsable ViewGroups) will shrink to accommodate the soft keyboard. But, if you set android:windowFullscreen="true" in the activity’s theme, then the ScrollView won’t shrink because it’s forced to fill the whole screen. However, setting android:fitsSystemWindows="false" in your theme also causes adjustResize not to work

护你周全 2024-10-16 19:34:33

我的 AndroidManifest 中有这个。它导致 adjustPan 停止正常工作。我删除了下面的块,一切又恢复正常了。

<supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="false"
        android:anyDensity="false" />

I had this in my AndroidManifest. It caused the adjustPan to stop working correctly. I removed the block below and everything works fine again.

<supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="false"
        android:anyDensity="false" />
心碎无痕… 2024-10-16 19:34:33

这是我找到的一种解决方法。打开有问题的 editText 并按 RETURN 键。请注意,它将 editText 移动到更靠近您要拍摄的位置。

因此,虽然很hacky,但您基本上可以在编辑文本的顶部添加换行符。

这似乎也可以在底部使用换行符,但您必须使用延迟才能添加换行符,直到软键盘动画到位后。

请注意,我仅在某些手机 (DroidX) 上遇到此问题。

if (android.os.Build.MODEL.equals("DROIDX")) {
        inputEt.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                    String text = inputEt.getText().toString();
                    text = "\n\n" + text.trim();
                    inputEt.setText(text);  
                    inputEt.setSelection(text.length());
                }
            });
        }

Here is one workaround I have found. Open the problematic editText and hit the RETURN key. Notice it shifts the editText closer to the position you're shooting for.

So although hacky, you can essentially please a newline at the top of the edittext.

This also seems to work using a newline at the bottom but you'd have to use a delay to not add the newline until AFTER the soft keyboard has animated into position.

Note I only have this problem on certain phones (DroidX).

if (android.os.Build.MODEL.equals("DROIDX")) {
        inputEt.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                    String text = inputEt.getText().toString();
                    text = "\n\n" + text.trim();
                    inputEt.setText(text);  
                    inputEt.setSelection(text.length());
                }
            });
        }
-柠檬树下少年和吉他 2024-10-16 19:34:33

尝试在清单中添加 android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden"

Try adding android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden" in your manifest.

笙痞 2024-10-16 19:34:33

您可以尝试以下设置:

<supports-screens
android:anyDensity="true"/>

You can try the following settings:

<supports-screens
android:anyDensity="true"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文