EditText in Listview loses focus when pressed on A

2020-01-26 05:36发布

I know there are a lot of similar questions out here but I couldn't get any of the provided solutions working in a simple sample app.

The problem occurs when the softkeyboard is shown for the first time. As soon as it is shown, only by pressing the editText again makes it editable.

Tried the following:

 android:windowSoftInputMode="adjustPan|adjustResize"

This is not solving any issues. It seems that this line is mandatory to have the activity resized after the softkeyboard is popping up. Unfortunately, it's also causing any EditTexts to lose focus. This is probably to the ListView itself gaining focus after the resizing process. So I tried the following workaround:

 listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

This always causes the first visible EditText that the ListView contains to gain focus, which is undesirable. The second EditText in the second row should instead gain focus when pressed, which is not happening. Also, if I eventually managed to focus another EditText other then the first one shown (e.g. by pressing 'Next' on the softkeyboard), the first visible one will receive focus after the keyboard is dismissed and the ListView being resized to its full size again.

I tried several other things like intercepting onFocusChange() events for the ListView, while knowing which EditText was pressed by its TouchListener. Requesting the focus for that certain EditText again did not lead to any success either.

Using a ScrollView instead of a ListView as suggested by other users is not an option either for the concerned project.

9条回答
贪生不怕死
2楼-- · 2020-01-26 06:29

When the list is long enough to cover the soft keyboard, the EditText in Listview loses focus when pressed on Android 4.x.

One solution is to wrap the Listview in a linear layout with a height of half the screen.

Whenever the Listview doesn't cover the softkeyboard, everything is fine.

查看更多
The star\"
3楼-- · 2020-01-26 06:31

In my case I've added local value currentlyFocusedRow in my Adapter. In the method getView() I've add these code to each editText:

   if (currentlyFocusedRow == position) {
        editText.requestFocus();
   }

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (currentlyFocusedRow == -1) {
                        currentlyFocusedRow = position;
                    }
                } else {
                    currentlyFocusedRow = -1;
                }
            }
        });
查看更多
疯言疯语
4楼-- · 2020-01-26 06:36

I was having problems with the ActionBar "stealing" focus when I pressed on an EditText located within a ListView row. The above solutions did not work, but the following solution worked for me:

http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html

Basically I added this to my ListView:

android:descendantFocusability="beforeDescendants"

and added this to my activity:

android:windowSoftInputMode="adjustPan"
查看更多
登录 后发表回答