Exception when focusing a EditText in a PopupWindow running on Device

后端 未结 7 2061
别那么骄傲
别那么骄傲 2021-02-12 22:38

I\'m developing a PopUp window for Android, and it\'s working, I added a EditText and a Button on that, when running on ADV this work properly, while running on device, when I f

7条回答
  •  悲哀的现实
    2021-02-12 23:24

    The cause:

    I trace the cause of the error down to the spelling auto-correct behavior on some phones (Moto Droid Razr and few other Moto Phones) and some android Rom (like some CM7 and CM7.1 ROMS). If the text contain a word that is incorrectly spell, and the text cursor is hovering in or near the text, the android OS will try automatically bring up the keyboard and try to provide suggestions on a correct spelling.

    On most devices, the auto correct suggestion box only appear as a one line segment above the keyboard. However on some custom ROMs (CM7 being one I seem a lot happening to) and some devices (Droid Razr), there is an additional drop down selection box that appear :

    See this image of what the auto-correct popup looks like (sorry not enough rep to insert image)

    I highly suspect that the auto correct drop-down list is also implemented as a popup window, and it is trying to use the current popup (the one containing the EditText with the mis-spell word) as the root view, and trying to get the windowToken from the root view.

    Since the popup itself is not a traditional view, I am assuming it is unable to give the correct windowToken to other views who are asking for them, therefore leading to the error.

    The Solutions:

    1) The easiest way I been able to get around this problem is by using Dialog instead of Popup windows. Their API is really similar and in my cases are fairly easy to replace PopupWindow using Dialog.

    For example:

    Old code:

        LayoutInflater inflater = (LayoutInflater) parentActivity.getLayoutInflater();
        View mainView = parentActivity.findViewById(R.id.main_calendar_fragment);
        updateEventPopupWindow = new PopupWindow(inflater.inflate(
                R.layout.add_event_fragment, null, false), metrics.widthPixels, metrics.heightPixels, true);
        updateEventPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        updateEventPopupWindow.showAtLocation(mainView, Gravity.CENTER, 0, 0);
    

    New code:

        LayoutInflater inflater = (LayoutInflater) parentActivity.getLayoutInflater();
        View mainView = parentActivity.findViewById(R.id.main_calendar_fragment);       
        updateEventDialog = new Dialog(parentActivity, android.R.style.Theme_NoTitleBar);
        updateEventDialog.setContentView(inflater.inflate(R.layout.add_event_fragment, (ViewGroup) mainView, false));
        updateEventDialog.show();
    

    2) The second approach is harder, but might be a suitable if PopupWindow to Dialog replacement is not doable, is to user Fragments in place of PopupWindows. There are many good fragment tutorials out there, so I won't bother to go over how to do this in this post.

    3) As a last resort, like multiple posters mentioned above, you can turn-off text auto correct on the EditText fields inside the PopupWindwow to side step this problem. However this lead to horrible user experiences, since many users (and keyboards like swype) relies on auto-correct, doing this will likely drive user away from your application.

    Hope this helps other out there who are facing this problem. I banged my head against the keyboard for over a day before finally decided to try the Dialog approach, which to my surprise was fairly easy to swap out. Best of luck to you

提交回复
热议问题