diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java index 763e077411..c3d603fd58 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java @@ -127,6 +127,16 @@ public class Cocos2dxEditBox extends EditText { private int mInputModeConstraints; private int mMaxLength; + public Boolean getChangedTextProgrammatically() { + return changedTextProgrammatically; + } + + public void setChangedTextProgrammatically(Boolean changedTextProgrammatically) { + this.changedTextProgrammatically = changedTextProgrammatically; + } + + private Boolean changedTextProgrammatically = false; + //OpenGL view scaleX private float mScaleX; diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java index b165ad2f27..32bed28d51 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java @@ -126,7 +126,7 @@ public class Cocos2dxEditBoxHelper { lParams.gravity = Gravity.TOP | Gravity.LEFT; mFrameLayout.addView(editBox, lParams); - + editBox.setTag(false); editBox.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -134,22 +134,25 @@ public class Cocos2dxEditBoxHelper { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - //The optimization can't be turn on due to unknown keyboard hide in some custom keyboard -// mFrameLayout.setEnableForceDoLayout(false); - // Note that we must to copy a string to prevent string content is modified - // on UI thread while 's.toString' is invoked at the same time. - final String text = new String(s.toString()); - mCocos2dxActivity.runOnGLThread(new Runnable() { - @Override - public void run() { - Cocos2dxEditBoxHelper.__editBoxEditingChanged(index, text); - } - }); } + //http://stackoverflow.com/questions/21713246/addtextchangedlistener-and-ontextchanged-are-always-called-when-android-fragment + @Override - public void afterTextChanged(Editable s) { + public void afterTextChanged(final Editable s) { + if (!editBox.getChangedTextProgrammatically()) { + if (!s.toString().equals("") && (Boolean) editBox.getTag()) { + mCocos2dxActivity.runOnGLThread(new Runnable() { + @Override + public void run() { + Cocos2dxEditBoxHelper.__editBoxEditingChanged(index, s.toString()); + } + + }); + } + } + editBox.setChangedTextProgrammatically(false); } }); @@ -159,6 +162,8 @@ public class Cocos2dxEditBoxHelper { @Override public void onFocusChange(View v, boolean hasFocus) { + editBox.setTag(true); + editBox.setChangedTextProgrammatically(false); if (hasFocus) { mCocos2dxActivity.runOnGLThread(new Runnable() { @Override @@ -344,7 +349,10 @@ public class Cocos2dxEditBoxHelper { public void run() { Cocos2dxEditBox editBox = mEditBoxArray.get(index); if (editBox != null) { + editBox.setChangedTextProgrammatically(true); editBox.setText(text); + int position = text.length(); + editBox.setSelection(position); } } }); diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp index c9eae32f18..bfa0a32970 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp @@ -57,6 +57,7 @@ EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText) , _colPlaceHolder(Color3B::GRAY) , _maxLength(-1) , _alignment(TextHAlignment::LEFT) +, _editingMode(false) { } @@ -232,16 +233,14 @@ void EditBoxImplCommon::refreshInactiveText() setInactiveText(_text.c_str()); refreshLabelAlignment(); - - if(_text.size() == 0) - { - _label->setVisible(false); - _labelPlaceHolder->setVisible(true); - } - else - { - _label->setVisible(true); - _labelPlaceHolder->setVisible(false); + if (!_editingMode) { + if (_text.size() == 0) { + _label->setVisible(false); + _labelPlaceHolder->setVisible(true); + } else { + _label->setVisible(true); + _labelPlaceHolder->setVisible(false); + } } } @@ -253,9 +252,11 @@ void EditBoxImplCommon::refreshLabelAlignment() void EditBoxImplCommon::setText(const char* text) { - this->setNativeText(text); - _text = text; - refreshInactiveText(); + if (nullptr != text) { + this->setNativeText(text); + _text = text; + refreshInactiveText(); + } } void EditBoxImplCommon::setPlaceHolder(const char* pText) @@ -263,9 +264,8 @@ void EditBoxImplCommon::setPlaceHolder(const char* pText) if (pText != NULL) { _placeHolder = pText; - _labelPlaceHolder->setString(_placeHolder); - this->setNativePlaceHolder(pText); + _labelPlaceHolder->setString(_placeHolder); } } @@ -308,7 +308,7 @@ void EditBoxImplCommon::openKeyboard() { _label->setVisible(false); _labelPlaceHolder->setVisible(false); - + _editingMode = true; this->setNativeVisible(true); this->nativeOpenKeyboard(); } @@ -316,12 +316,13 @@ void EditBoxImplCommon::openKeyboard() void EditBoxImplCommon::closeKeyboard() { this->nativeCloseKeyboard(); + _editingMode = false; } void EditBoxImplCommon::onEndEditing(const std::string& /*text*/) { + _editingMode = false; this->setNativeVisible(false); - refreshInactiveText(); } diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-common.h b/cocos/ui/UIEditBox/UIEditBoxImpl-common.h index 3e50b1c242..d58cb24331 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-common.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-common.h @@ -157,6 +157,7 @@ private: int _maxLength; Size _contentSize; + bool _editingMode; }; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp index dfd6251839..958d69ca8c 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp @@ -152,6 +152,7 @@ void UIEditBoxTest::editBoxEditingDidEndWithAction(cocos2d::ui::EditBox* editBox void UIEditBoxTest::editBoxTextChanged(cocos2d::ui::EditBox* editBox, const std::string& text) { log("editBox %p TextChanged, text: %s ", editBox, text.c_str()); + editBox->setText(text.c_str()); } void UIEditBoxTest::editBoxReturn(ui::EditBox* editBox)