When the soft keyboard appears, it makes my EditText field lose focus

后端 未结 11 1757
名媛妹妹
名媛妹妹 2020-12-07 10:19

I\'ve got a few EditText fields in a ListView. When I tap on one of the EditText fields, the keyboard slides into view (as it should), but the EditText field I tapped loses

相关标签:
11条回答
  • 2020-12-07 10:47

    In my case, I had called root_scrollview.fullScroll(View.FOCUS_DOWN) on my root ScrollView when Keyboard appears. I replaced it with

    login_scrollview.post(new Runnable() { 
        @Override
        public void run() {
            root_scrollview.scrollTo(0,root_container.bottom)
        }
    });
    

    where root_container is the immediate child of root_scrollview. This solved the problem for me.

    Note: Directly calling root_scrollview.scrollTo(0,root_container.bottom) was not working.

    0 讨论(0)
  • 2020-12-07 10:48

    For those who come here with Xamarin or Xamarin.Forms:

    I had the same issue as well but only with Android 5.x - all newer Versions including 8.1 worked well.

    Obviously sheltond was right by saying:

    In my case, this is happening because when the ListView resizes, it re-creates all of the list items (i.e. it calls getView() again for each visible list item).

    My listview was resizing as well and no, Franks solution to set windowSoftInputMode="adjustPan" was no option for me because that means that the keyboard moves the listview partly off the screen.

    All I had to do after hours of focus-debugging was setting the cell caching strategy of the Xamarin Forms ListView:

    From

    CachingStrategy="RecycleElement"
    

    To

    CachingStrategy="RetainElement"
    

    This will stop the cells from being recreated. However, this might result in bad performance and high memory consumption for huge lists. Be aware.

    0 讨论(0)
  • 2020-12-07 10:50

    You should test this code on a device with hardware keyboard always visible. The behavior may also happen here.

    To avoid this you can have the keyboard always visible.. but that is not very easy as you can see by this thread:

    https://groups.google.com/forum/#!topic/android-developers/FyENeEdmYC0

    Theoretically you may have to create your own Android keyboard (although using as base the stock Android keyboard) as described here: Android: How to make the keypad always visible?

    0 讨论(0)
  • 2020-12-07 10:53

    You need to change in your AndroidManifest.xml

    Add android:windowSoftInputMode="adjustPan" in the activity holding the listview. This will solve your problem.

        <activity android:name=".MyEditTextInListView"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustPan">
    

    Regards

    0 讨论(0)
  • 2020-12-07 10:55

    If the editText inside the listView just make sure that you inflate the View in the getView method with this way.


            if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.yourItemListLayout,
                    parent, false);   
    

    Edit: this work for some mobiles not all I use the answer from Mr.Frank above.

    0 讨论(0)
提交回复
热议问题