Why the soft keyboard shows or not when an activity starts?

懵懂的女人 提交于 2019-12-03 14:00:34

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

IRPdevelop

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!