Fix android edit box duplicated text (#17775)

* fix editbox textChanged event called after fragment loaded

* fix editbox duplicatd text when change text in textChanged callback

* revert changes of placeholder modification
This commit is contained in:
子龙山人 2017-05-04 15:04:47 +08:00 committed by minggo
parent 973d6aa955
commit 55b14cbde5
5 changed files with 51 additions and 30 deletions

View File

@ -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;

View File

@ -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);
}
}
});

View File

@ -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();
}

View File

@ -157,6 +157,7 @@ private:
int _maxLength;
Size _contentSize;
bool _editingMode;
};

View File

@ -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)