弹出软键盘时页面滚动

问题描述 投票:87回答:10

我有一个<ScrollView>布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

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

        <EditText
            android:id="@+id/input_one"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:inputType="number" >

         <EditText
            android:id="@+id/input_two"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:inputType="number" >

         <EditText
            android:id="@+id/input_three"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:inputType="number" >

          <Button
            android:id="@+id/ok_btn"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="@string/ok_str" />

      </LinearLayout>
</ScrollView>

如上所示,简单布局包含三个输入字段和一个“确定”按钮。

我使用上面的布局运行我的应用程序,当我点击第一个输入字段(@+id/input_one)时,软键盘将从屏幕底部弹出,它隐藏第三个输入字段和“确定”按钮。

因为我使用<ScrollView>,我想我可以向上滚动页面以查看第三个输入字段和软键盘隐藏的“确定”按钮,但页面不可滚动。为什么?如何摆脱它?基本上,我想看到每个输入字段和“确定”按钮,甚至软键盘弹出。

android android-layout android-intent android-scrollview android-keypad
10个回答
114
投票

我通过在AndroidManifest.xml的<activity>中定义以下属性来解决问题

android:windowSoftInputMode="stateVisible|adjustResize"

0
投票

此外,如果您想以编程方式执行此操作,只需将以下行添加到活动的onCreate中。

getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | 
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE );

36
投票

好的,我现在已经搜索了几个小时来找到问题,我找到了。

fillViewport="true"android:windowSoftInputMode="adjustResize"这些变化都没有帮助我。

我使用的是Android 4.4,这是一个很大的错误:

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

不知何故,当软键盘可见时,全屏主题会阻止ScrollView滚动。

目前,我最终使用了这个:

android:theme="@android:style/Theme.Black.NoTitleBar

我不知道如何摆脱顶部系统栏的时间和电池显示,但至少我遇到了问题。

希望这有助于其他有同样问题的人。

编辑:我的案例有一点解决方法,所以我发布了它here

我不确定这是否适合你,但它肯定会帮助你理解,并清除一些事情。

EDIT2:Here是另一个很好的主题,可以帮助您朝着正确的方向前进,甚至解决您的问题。


16
投票

对我来说唯一有用的东西就是放入清单中的活动这个属性:

android:windowSoftInputMode="stateHidden|adjustPan"

打开活动时不显示键盘且不与视图底部重叠。


11
投票

在你的Manifest定义windowSoftInputMode属性<activity android:name=".MyActivity" android:windowSoftInputMode="adjustNothing">


11
投票

在没有全屏模式下将这个放在你的Manifest中

android:windowSoftInputMode="stateVisible|adjustPan" 

在你的清单中

<activity
    android:windowSoftInputMode="stateVisible|adjustPan"
    android:name="com.example.patronusgps.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

7
投票

在活动代码下的Android Manifest文件中添加以下代码行

android:windowSoftInputMode="adjustResize"

5
投票

看看这个。

<activity android:name=".Calculator" android:windowSoftInputMode="stateHidden|adjustResize" android:theme="@android:style/Theme.Black.NoTitleBar"></activity>

3
投票

这对我有用:

android:windowSoftInputMode="adjustPan"

1
投票

您可以尝试使用以下代码来解决您的问题:

 <activity
     android:name=".DonateNow"
     android:label="@string/title_activity_donate_now"
     android:screenOrientation="portrait"
     android:theme="@style/AppTheme"
     android:windowSoftInputMode="stateVisible|adjustPan">
 </activity>
© www.soinside.com 2019 - 2024. All rights reserved.