From 7cbaccffd63126e805a6f0dcd2ab93cbd0f61761 Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Thu, 27 Apr 2017 12:12:42 -0700 Subject: [PATCH] Fixed some issues with switching android text fields. The improvement to faster field switching (https://github.com/codenameone/CodenameOne/commit/f3e53a80704149e4d7cde276d01c1368bcdcfe2c) appears to hae caused a couple of regressions. Fixed so that the resize mode (resize or pan) is re-evaluated when the field is switched. This fixes an issue with the resize mode being fine for the first field, but being inappropriate for the next field. E.g. in a login dialog you can edit the username, but then the password is covered by a button. Also fixed so that the on finished editing event is fired in the first text field when changing to the next text field. --- .../impl/android/InPlaceEditView.java | 94 ++++++++++++------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/Ports/Android/src/com/codename1/impl/android/InPlaceEditView.java b/Ports/Android/src/com/codename1/impl/android/InPlaceEditView.java index 742a8fab4d..1bf72e64b5 100644 --- a/Ports/Android/src/com/codename1/impl/android/InPlaceEditView.java +++ b/Ports/Android/src/com/codename1/impl/android/InPlaceEditView.java @@ -810,7 +810,7 @@ else if (isEditedFieldSwitch) { } } if (!isEditedFieldSwitch) { - mEditText.addTextChangedListener(mEditText.mTextWatcher); + mEditText.addTextChangedListener(mEditText.mTextWatcher); } mEditText.setBackgroundDrawable(null); @@ -870,7 +870,7 @@ else if (isEditedFieldSwitch) { mEditText.setHint(textArea.getHint()); } if (!isEditedFieldSwitch) { - addView(mEditText, mEditLayoutParams); + addView(mEditText, mEditLayoutParams); } invalidate(); setVisibility(VISIBLE); @@ -1353,6 +1353,7 @@ public static void edit(final AndroidImplementation impl, final Component compon final TextArea textArea = (TextArea) component; final String initialText = textArea.getText(); + textArea.putClientProperty("InPlaceEditView.initialText", initialText); Dimension prefSize = textArea.getPreferredSize(); @@ -1428,37 +1429,55 @@ public static void edit(final AndroidImplementation impl, final Component compon if (impl.isAsyncEditMode()) { isEditedFieldSwitch = true; + final String[] out = new String[1]; + TextArea prevTextArea = null; + if(sInstance != null && sInstance.mLastEditText != null) { + prevTextArea = sInstance.mLastEditText.getTextArea(); + } + + if (prevTextArea != null) { + final TextArea fPrevTextArea = prevTextArea; + final String retVal = sInstance.mLastEditText.getText().toString(); + Display.getInstance().callSerially(new Runnable() { + public void run() { + Display.getInstance().onEditingComplete(fPrevTextArea, retVal); + } + }); + } + + InPlaceEditView.setEditedTextField(textArea); + nextTextArea = null; } else { isEditedFieldSwitch = false; - final InPlaceEditView instance = sInstance; - if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) { - instance.showTextEditorAgain(); - return; - } - if (!isClosing && sInstance != null && sInstance.mEditText != null) { - isClosing = true; + final InPlaceEditView instance = sInstance; + if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) { + instance.showTextEditorAgain(); + return; + } + if (!isClosing && sInstance != null && sInstance.mEditText != null) { + isClosing = true; + + impl.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + instance.endEditing(REASON_UNDEFINED, true); + } + }); + } + + afterClose = new Runnable() { - impl.getActivity().runOnUiThread(new Runnable() { @Override public void run() { - instance.endEditing(REASON_UNDEFINED, true); + impl.callHideTextEditor(); + Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText()); } - }); - } - - afterClose = new Runnable() { - @Override - public void run() { - impl.callHideTextEditor(); - Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText()); - } - - }; - return; + }; + return; } } else { isEditedFieldSwitch = false; @@ -1485,25 +1504,26 @@ public void run() { @Override public void run() { if (!isEditedFieldSwitch) { - releaseEdit(); + releaseEdit(); - if (sInstance == null) { - sInstance = new InPlaceEditView(impl); - impl.relativeLayout.addView(sInstance); - } + if (sInstance == null) { + sInstance = new InPlaceEditView(impl); + impl.relativeLayout.addView(sInstance); + } - // Let's try something new here - // We'll ALWAYS try resize edit mode (since it just works better) - // But we'll detect whether the field is still covered by the keyboard - // and switch to pan mode if necessary. + // Let's try something new here + // We'll ALWAYS try resize edit mode (since it just works better) + // But we'll detect whether the field is still covered by the keyboard + // and switch to pan mode if necessary. + + } if(scrollableParent || parentForm.isFormBottomPaddingEditingMode()){ setEditMode(true); }else{ trySetEditMode(true); } - } sInstance.startEditing(impl.getActivity(), textAreaData, initialText, inputType, isEditedFieldSwitch); } }); @@ -1511,6 +1531,8 @@ public void run() { final String[] out = new String[1]; + + // In order to reuse the code the runs after edit completion, we will wrap it in a runnable // For sync edit mode, we will just run onComplete.run() at the end of this method. For // Async mode we add the Runnable to the textarea as a client property, then run it @@ -1662,10 +1684,14 @@ private class TextChange { } - private class EditView extends AutoCompleteTextView { + class EditView extends AutoCompleteTextView { private InPlaceEditView mInPlaceEditView; private TextArea mTextArea = null; + + TextArea getTextArea() { + return mTextArea; + } private ResetableTextWatcher mTextWatcher = new ResetableTextWatcher() { private boolean started = false;