Why doesn't android:windowSoftInputMode=“state

2019-02-10 09:35发布

I can't seem to make the android:windowSoftInputMode="stateVisible|adjustResize" option work. When the soft keyboard shows, the scroll view doesn't automatically scroll to the bottom part.

Edit: I tried using adjustPan instead (stateVisible|adjustPan) but what happens is the scroll view gets disabled.

Solution: Finally, I found a suggestion that works. I created an OnGlobalLayoutListener() and added it to my scroll view. I checked if the height of the root view of my activity(which is my scroll view) changed. If yes, I'm assuming that the soft keyboard is shown.

Click here for more info.

Here's my source code:

AndroidManifest.xml

<application
        ...
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            ...
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateVisible|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ...
</application>

Login Screen with keyboard - scroll view does not scroll enter image description here

Desired result enter image description here

5条回答
混吃等死
2楼-- · 2019-02-10 10:09

I found that if I had set layout parameters on my scroll view, then it would not work. As soon as a removed the layout parameters it worked as expected. I can't explain why, just letting others know what worked for me.

查看更多
ら.Afraid
3楼-- · 2019-02-10 10:10

In Manifest:

android:windowSoftInputMode="adjustPan|stateVisible"

In layout xml bottom of all child layout of scrollview set one textview, such as

  <ScrollView
    android:id="@+id/driverLoginScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="false"
    android:scrollbars="none">
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="50dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">
        .
        .
        .
       <TextView
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:focusable="false"
       android:textIsSelectable="false" />
       </LinearLayout>
</ScrollView>
查看更多
淡お忘
4楼-- · 2019-02-10 10:12

try

android:windowSoftInputMode="adjustPan|stateVisible"
查看更多
看我几分像从前
5楼-- · 2019-02-10 10:18

You could automatically scroll to the bottom of your activity by yourself, by overriding the onConfigurationChanged() callback in you Activity :

scrollView.post(new Runnable() {            
    @Override
    public void run() {
           scrollView.fullScroll(View.FOCUS_DOWN);              
    }
});

This means you also have to tell your activity you want the callback to be called when the activity changes its layout by using the configChanges attribute in the activity declaration in your AndroidManifest file :

<activity
    android:configChanges="screenLayout|screenSize" <!-- Don't know which could actually do the trick -->
    ... >
</activity>

I hope this can help you.

查看更多
再贱就再见
6楼-- · 2019-02-10 10:19

Solution: Finally, I found a suggestion that works. I created an OnGlobalLayoutListener() and added it to my scroll view. I checked if the height of the root view of my activity(which is my scroll view) changed. If yes, I'm assuming that the soft keyboard is shown.

Click here for more info.

查看更多
登录 后发表回答