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>

但是当我们添加 aScrollView时,默认情况下会显示软键盘。

<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,但这是默认行为。

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

4

3 回答 3

3

这是我在挖掘 Android 代码并使用EditText.

ScrollView定义为

 public class More ...ScrollView extends FrameLayout { ... }

我尝试使用 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>

但正如问题中所写,使用 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 的类并使用代码之后,我发现它是默认滚动视图样式(R.attr.scrollViewStyle)中的东西,它负责显示或不显示键盘......

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

于 2013-09-10T21:20:18.327 回答
1

就我而言, android:scrollbars 修复了这个问题,直到我不得不添加:

android:windowSoftInputMode="adjustResize"> 

能够在键盘显示时滚动。

为了能够使用这两个属性,我必须添加:

android:focusableInTouchMode="true"

在 Scrollview 的子项中

我在这里找到了 focusableInTouchMode 答案: Stop EditText from getting focus at Activity startup

于 2016-05-09T01:20:18.747 回答
-1

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

于 2013-09-10T14:34:48.593 回答