当一个活动开始时,为什么软键盘会出现或不出现?

7

在开发人员之间比较我们的设计时,我们发现了一种奇怪的行为。经过一些分析,我们得出了这个观察结果。

当活动开始时,在某些情况下会出现键盘,但有时不会。

实际上,在没有 ScrollView 的情况下,软键盘不会默认出现在 EditText 上。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</LinearLayout>

但是当我们添加一个ScrollView时,默认情况下软键盘会出现。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text" >
            <requestFocus />
        </EditText>
    </ScrollView>
</LinearLayout>

这只取决于ScrollView的存在。我们可以通过在AndroidManifest中进行特定声明来解决这个问题,但这是默认行为。

我和我的同事开发人员想知道为什么会出现这种情况?


我也注意到了这个问题,但是找不到答案。 - tyczj
3个回答

3
以下是我在研究Android代码并构建一些带有EditText的测试布局后对此问题的理解:
由于ScrollView的定义为:
 public class More ...ScrollView extends FrameLayout { ... }

我尝试使用FrameLayout作为EditText项目的容器。结果软键盘没有被触发。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</FrameLayout>

但是根据问题描述,使用ScrollView会触发软键盘(我简化了xml源代码)。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</ScrollView>

因此,触发软件键盘的元素位于ScrollView源文件中。
编辑:在创建了自己的类MyFrameLayout并扩展了FrameLayout并尝试了代码之后,我发现是默认scrollview样式(R.attr.scrollViewStyle)中的某些内容负责显示或不显示键盘...
编辑2:最后,如果存在属性android:scrollbars,则允许在启动时自动触发键盘...

似乎是一个问题或奇怪的副作用。 - Solostaran14
使用设置 android:windowSoftInputMode="stateVisible" 不行吗? - Darpan

1
在我的情况下,android:scrollbars 修复了这个问题,直到我不得不添加以下内容:
android:windowSoftInputMode="adjustResize"> 

当键盘弹出时能够滚动。
为了同时使用这两个属性,我需要添加:
android:focusableInTouchMode="true"

在ScrollView的子控件中

我在这里找到了关于focusableInTouchMode的答案:阻止EditText在Activity启动时获取焦点


-1
这是因为当应用程序启动时,Android 会专注于第一个可用的视图。在第一种情况下,它是 EditText,这就是为什么键盘会弹出的原因。在第二种情况下,第一个视图是 ScrollView,它不需要键盘,因此不会显示。 此外,在第一种情况下,您可以删除 <requestFocus />,在某些设备上,键盘将不会弹出。希望这有所帮助。

1
不,你搞反了,当键盘在滚动视图中时它会显示出来,但当它不在滚动视图中时则不会显示。 - tyczj
嗯,这很奇怪。我得查一下。 - ya-ivanov
确实,requestFocus不是必须的。但问题在其他地方。 - Solostaran14

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接