Why the soft keyboard shows or not when an activit

2019-03-27 15:46发布

When comparing our design between developers, we found a strange behavior. After some analysis we went to this observation.

When the activity starts, on some cases the keyboard appears but sometimes not.

In fact, without a ScrollView, the soft keyboard does not appear by default on an 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>

But when we add a ScrollView, the soft keyboard shows up by default.

<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>

It only depends on the presence of the ScrollView. We can fix that with a specific declaration in the AndroidManifest, but this is the default behavior.

I and my fellow developer wonder why is this occurring ?

3条回答
等我变得足够好
2楼-- · 2019-03-27 15:54

This is because when and app is launched, android focuses on the first available view. In the first case it is the EditText, thats why the keyboard pops up. In the second case, the first view is the ScrollView is the first view, which doesn't require keyboard, so it is not shown. Also, in the first case, you can remove <requestFocus />, and on some devices, the keyboard, will not pop up. Hope this helps.

查看更多
你好瞎i
3楼-- · 2019-03-27 15:58

Here is what I understand of this problem after digging in the code of Android and building some test layouts with an EditText.

As ScrollView is defined as

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

I tried using a FrameLayout as a container for an EditText item. As a result the software keyboard is not triggered.

<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>

But as written in the question, using a ScrollView triggers the software keyboard (I simplified the xml source).

<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>

So the element that allows the software keyboard to be triggered is in the ScrollView source file.

Edit: after having created my own class MyFrameLayout extending FrameLayout and playing with the code, I found that it is something in default scrollview style (R.attr.scrollViewStyle) that is responsible for the keyboard to be shown or not...

Edit2: finally the attribute android:scrollbars allows the keyboard to be automatically triggered at startup if present...

查看更多
唯我独甜
4楼-- · 2019-03-27 16:03

In my case android:scrollbars fixed this until I had to add:

android:windowSoftInputMode="adjustResize"> 

To be able to scroll when keyboard shows.

To be able to use both properties I had to add:

android:focusableInTouchMode="true"

In the child of the Scrollview

I found focusableInTouchMode answer here: Stop EditText from gaining focus at Activity startup

查看更多
登录 后发表回答