EditText weird behaviour in ListView BaseAdapter

2019-06-24 06:38发布

问题:

I got a ListView, populated using BaseAdapter. In the listview Item there's a numeric EditText:

...
            <EditText
                android:id="@+id/edit_quantita"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:background="@drawable/edit_quantita"
                android:gravity="center_vertical|center_horizontal"
                android:inputType="number"
                android:text="1"
                android:textColor="#fff"
                tools:ignore="HardcodedText" >

            </EditText>
...

When I tap on this EditText the numerical keyboard prompts for an instant, and then it's suddenly overlayed by a regular character keyboard. If I try to write something on this keyboard no text is shown anywhere. Curiously, if I tap again on the Editext it behaves as it should, showing only the working numerical keyboard.

What can i do?

回答1:

The implementation of EditText has many flaws when used in the ListView. Try to add this piece of code in your onCreate():

int apiVersion = android.os.Build.VERSION.SDK_INT;
if (apiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
else
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


回答2:

You can try the following:

  • Check if you have another EditText that might be overlapped with the numeric one.
  • Create a new activity with only a numeric EditText and see if the same happens.
  • Check the activity's code to see if something is being modified on run time. I don't think you could change the EditText type via code, but maybe something else is modifying it's behavior.

Hope it helps debug the problem.