45

我正在尝试创建一个非常基本的聊天屏幕,其中显示文本的 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>

4

7 回答 7

54

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

于 2011-01-25T04:10:41.067 回答
32

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

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

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

  • stateUnspecified 未指定软键盘的状态(隐藏或可见)。系统会选择一个合适的状态或者依赖于主题中的设置。 这是软键盘行为的默认设置。
  • stateUnchanged 当活动出现时,软键盘将保持在它最后的任何状态,无论是可见的还是隐藏的。
  • “stateHidden” 当用户选择活动时,软键盘被隐藏——也就是说,当用户肯定地向前导航到活动时,而不是因为离开另一个活动而返回到活动。
  • stateAlwaysHidden 当 Activity 的主窗口有输入焦点时,软键盘总是隐藏。
  • stateVisible 软键盘在正常情况下是可见的(当用户向前导航到活动的主窗口时)。
  • stateAlwaysVisible 软键盘在用户选择活动时可见——即当用户肯定地向前导航到活动时,而不是因为离开另一个活动而返回到活动。
  • adjustUnspecified 未指定 Activity 的主窗口是否调整大小以便为软键盘腾出空间,或者是否平移窗口的内容以使当前焦点在屏幕上可见。系统会根据窗口的内容是否有任何可以滚动其内容的布局视图来自动选择其中一种模式。如果存在这样的视图,则窗口将被调整大小,假设滚动可以使窗口的所有内容在较小的区域内可见。这是主窗口行为的默认设置。
  • adjustResize Activity 的主窗口总是调整大小,以便为屏幕上的软键盘腾出空间。
  • adjustPan Activity 的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。
于 2014-01-22T20:53:19.263 回答
10

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

于 2015-10-27T14:37:59.737 回答
1

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

<supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="false"
        android:anyDensity="false" />
于 2014-09-10T21:45:07.573 回答
0

这是我发现的一种解决方法。打开有问题的 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());
                }
            });
        }
于 2011-10-08T21:34:23.983 回答
-1

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

于 2015-06-20T15:39:21.790 回答
-14

您可以尝试以下设置:

<supports-screens
android:anyDensity="true"/>
于 2015-07-12T15:33:48.680 回答