调整不要阻止键盘覆盖EditText

我正在尝试创build一个非常基本的聊天画面,ListView显示文本,底部有一个EditText,EditText右侧有一个“发送”button。 一切都正常,但是当我点击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> 

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

在你的清单文件中,你需要设置合适的android:windowSoftInputMode属性。 此属性从API 3开始有效。

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

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

  • stateUnspecified软键盘的状态(无论是隐藏还是可见)未指定。 系统会select一个合适的状态或依靠主题中的设置。 这是软键盘行为的默认设置。
  • stateUnchanged当活动出现时,软键盘保持在最后一个状态,无论是可见的还是隐藏的。
  • “stateHidden”软键盘在用户select活动时隐藏 – 也就是说,当用户肯定地向前导航到活动时,而不是由于离开另一个活动而回退。
  • stateAlwaysHidden当活动的主窗口具有input焦点时,软键盘始终处于隐藏状态。
  • stateVisible当正常情况下(当用户向前导航到活动的主窗口时),软键盘是可见的。
  • stateAlwaysVisible当用户select活动时,软键盘是可见的 – 也就是说,当用户肯定地向前导航到活动时,而不是由于离开另一个活动而回退。
  • adjustUnspecified未指定活动的主窗口是否调整为软键盘腾出空间,或窗口内容是否使当前焦点在屏幕上可见。 系统将自动select这些模式之一,具体取决于窗口的内容是否有任何可以滚动其内容的布局视图。 如果有这样的观点,窗口将被resize,假设滚动可以使窗口的所有内容在一个较小的区域内可见。 这是主窗口行为的默认设置。
  • adjustResize调整活动的主窗口的大小,为屏幕上的软键盘腾出空间。
  • adjustPan活动的主窗口不为软键盘调整空间。 相反,窗口的内容会自动平移,以便当前的焦点永远不会被键盘遮挡,用户总是可以看到他们正在键入的内容。 这通常不如resize,因为用户可能需要closures软键盘才能与窗口的遮蔽部分进行交互。

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

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

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

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

这是我find的一种解决方法。 打开有问题的editText并按下RETURN键。 请注意,它将editText转移到您正在拍摄的位置。

所以,尽pipehacky,你可以请editline顶部的换行符。

这也似乎工作在底部换行符,但你必须使用延迟不添加新行,直到软键盘animation到位后。

注意我在某些手机(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()); } }); } 

您可以尝试以下设置:

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