Why do the Android Keyboard pops up when adding a ListView

折月煮酒 提交于 2019-12-07 10:37:52

问题


I'm facing a weird problem when adding a ListView to my layout.

My layout contains 2 EditText and when I start the activity, the keyboard doesn't pop automatically. But when I add a ListView anywhere in the layout, the keyboard pop up when the activity starts.

I know there are many ways to hide the keyboard like this one: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) and you may have seen other solutions to the problems like here and here but my question is not how to prevent this but why is this happening ?

I have created a simple example to demonstrate this behavior

<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"
    android:background="@android:color/black"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="chadi.projects.code.TestKeyboardActivity" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@android:color/white" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

</LinearLayout>

If this ListView doesn't exist, the keyboard don't show up. If I replace the ListView by other views, it still doesn't show up. Only when I add a simple ListView (even wihtout populating it with anything), is the keboard showing up.

Why is this happening?


回答1:


Try this on your manifest file

<activity
    android:name=".MyActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:windowSoftInputMode="stateHidden" /> 



回答2:


for that you have to add request focus to your edit text like this

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@android:color/white">
<requestFocus />
</EditText>


来源:https://stackoverflow.com/questions/30231829/why-do-the-android-keyboard-pops-up-when-adding-a-listview

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