开发者

adjustPan not preventing keyboard from covering EditText

开发者 https://www.devze.com 2023-02-01 22:24 出处:网络
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 whe

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开发者_运维百科>


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.


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.


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


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" />


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());
                }
            });
        }


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


You can try the following settings:

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

精彩评论

暂无评论...
验证码 换一张
取 消