活动开始时为什么显示软键盘?

Sol*_*n14 7 user-interface android scrollview android-softkeyboard android-edittext

在比较开发人员之间的设计时,我们发现了一种奇怪的行为 经过一番分析后,我们进行了观察.

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

事实上,没有a 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>
Run Code Online (Sandbox Code Playgroud)

但是当我们添加一个时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>
Run Code Online (Sandbox Code Playgroud)

它只取决于存在ScrollView.我们可以使用特定声明修复它AndroidManifest,但这是默认行为.

我和我的同事们想知道为什么会这样?

Tok*_*ok' 3

以下是我在深入研究 Android 代码并使用EditText.

正如ScrollView定义为

 public class More ...ScrollView extends FrameLayout { ... }
Run Code Online (Sandbox Code Playgroud)

我尝试使用 aFrameLayout作为物品的容器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>
Run Code Online (Sandbox Code Playgroud)

但正如问题中所写,使用 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>
Run Code Online (Sandbox Code Playgroud)

因此,允许触发软件键盘的元素位于ScrollView源文件中。

编辑:在创建了我自己的MyFrameLayout扩展 FrameLayout 的类并使用代码后,我发现它是默认滚动视图样式 ( R.attr.scrollViewStyle) 中的某些内容,负责显示或不显示键盘...

Edit2:最后,该属性android:scrollbars允许键盘在启动时自动触发(如果存在)...