简体   繁体   中英

Android keyboard only shows up once, but works perfectly when I remove the ListView

Current Behavior

Use the below xml layout, the android keyboard for the EditText immediately shows up when the activity starts. If I exit the keyboard, touching the EditText does nothing to bring the keyboard back.

Desired Behavior

When I comment out the ListView below, everything works perfectly. The android keyboard does not pop up immediately for the EditText, but rather only when I press it. I can also bring the keyboard back by touching the EditText.

Questions

  1. Why does the unintended behavior occur when I have the ListView?
  2. How can I keep the ListView and the desired behavior?

Side Notes

In both scenarios, I commented out all the Java code that dealt with the ListView, so this is not a programming issue. When I click the back button, and then come back to this activity, the keyboard pops up again (when the ListView is commented out).

<RelativeLayout 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: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="com.mywebsite.myproject.PrivateChat" >

<RelativeLayout
    android:id="@+id/form"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/message_input_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:background="#333333"
        android:hint="Type a message..."
        android:textColorHint="#FFFFFF"
        android:inputType="text"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:textColor="#FFFFFF"
        android:textSize="26sp" />

</RelativeLayout>

<ListView
    android:id="@+id/private_chat_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

I fixed it with only 2 lines of xml.

I added this line to the activity in AndroidManifest.xml :

android:windowSoftInputMode="stateAlwaysHidden" 

I added this line to the ListView:

android:layout_above="@id/form"

Why this works

I have a feeling there's a bug with ListViews automatically bringing up the soft keyboard. When adding the 1st line of xml, it stopped the keyboard from automatically popping up. According to the docs, all this does is The soft keyboard is always hidden when the activity's main window has input focus. In other words, the only way this line could possibly have effect is if the main window has focus, so it is most likely a bug with ListViews. Sorry I don't have a more concrete explanation.

The second line is intuitive. The ListView was taking up the entire screen since I set its height to match_parent . Therefore it was covering the EditText and that's why I couldn't press the EditText again to bring back the keyboard. To stop the ListView from overlapping with the EditText, I just anchored the ListView above the EditText (well above the RelativeLayout containing the EditText but that's not the point).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM