Skip to content

Commit 55b14cb

Browse files
zilongshanrenminggo
authored andcommitted
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
1 parent 973d6aa commit 55b14cb

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ public class Cocos2dxEditBox extends EditText {
127127
private int mInputModeConstraints;
128128
private int mMaxLength;
129129

130+
public Boolean getChangedTextProgrammatically() {
131+
return changedTextProgrammatically;
132+
}
133+
134+
public void setChangedTextProgrammatically(Boolean changedTextProgrammatically) {
135+
this.changedTextProgrammatically = changedTextProgrammatically;
136+
}
137+
138+
private Boolean changedTextProgrammatically = false;
139+
130140
//OpenGL view scaleX
131141
private float mScaleX;
132142

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,33 @@ public void run() {
126126
lParams.gravity = Gravity.TOP | Gravity.LEFT;
127127

128128
mFrameLayout.addView(editBox, lParams);
129-
129+
editBox.setTag(false);
130130
editBox.addTextChangedListener(new TextWatcher() {
131131
@Override
132132
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
133133
}
134134

135135
@Override
136136
public void onTextChanged(CharSequence s, int start, int before, int count) {
137-
//The optimization can't be turn on due to unknown keyboard hide in some custom keyboard
138-
// mFrameLayout.setEnableForceDoLayout(false);
139-
140-
// Note that we must to copy a string to prevent string content is modified
141-
// on UI thread while 's.toString' is invoked at the same time.
142-
final String text = new String(s.toString());
143-
mCocos2dxActivity.runOnGLThread(new Runnable() {
144-
@Override
145-
public void run() {
146-
Cocos2dxEditBoxHelper.__editBoxEditingChanged(index, text);
147-
}
148-
});
137+
149138
}
150139

140+
//http://stackoverflow.com/questions/21713246/addtextchangedlistener-and-ontextchanged-are-always-called-when-android-fragment
141+
151142
@Override
152-
public void afterTextChanged(Editable s) {
143+
public void afterTextChanged(final Editable s) {
144+
if (!editBox.getChangedTextProgrammatically()) {
145+
if (!s.toString().equals("") && (Boolean) editBox.getTag()) {
146+
mCocos2dxActivity.runOnGLThread(new Runnable() {
147+
@Override
148+
public void run() {
149+
Cocos2dxEditBoxHelper.__editBoxEditingChanged(index, s.toString());
150+
}
151+
152+
});
153+
}
154+
}
155+
editBox.setChangedTextProgrammatically(false);
153156

154157
}
155158
});
@@ -159,6 +162,8 @@ public void afterTextChanged(Editable s) {
159162

160163
@Override
161164
public void onFocusChange(View v, boolean hasFocus) {
165+
editBox.setTag(true);
166+
editBox.setChangedTextProgrammatically(false);
162167
if (hasFocus) {
163168
mCocos2dxActivity.runOnGLThread(new Runnable() {
164169
@Override
@@ -344,7 +349,10 @@ public static void setText(final int index, final String text){
344349
public void run() {
345350
Cocos2dxEditBox editBox = mEditBoxArray.get(index);
346351
if (editBox != null) {
352+
editBox.setChangedTextProgrammatically(true);
347353
editBox.setText(text);
354+
int position = text.length();
355+
editBox.setSelection(position);
348356
}
349357
}
350358
});

cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText)
5757
, _colPlaceHolder(Color3B::GRAY)
5858
, _maxLength(-1)
5959
, _alignment(TextHAlignment::LEFT)
60+
, _editingMode(false)
6061
{
6162
}
6263

@@ -232,16 +233,14 @@ void EditBoxImplCommon::refreshInactiveText()
232233
setInactiveText(_text.c_str());
233234

234235
refreshLabelAlignment();
235-
236-
if(_text.size() == 0)
237-
{
238-
_label->setVisible(false);
239-
_labelPlaceHolder->setVisible(true);
240-
}
241-
else
242-
{
243-
_label->setVisible(true);
244-
_labelPlaceHolder->setVisible(false);
236+
if (!_editingMode) {
237+
if (_text.size() == 0) {
238+
_label->setVisible(false);
239+
_labelPlaceHolder->setVisible(true);
240+
} else {
241+
_label->setVisible(true);
242+
_labelPlaceHolder->setVisible(false);
243+
}
245244
}
246245
}
247246

@@ -253,19 +252,20 @@ void EditBoxImplCommon::refreshLabelAlignment()
253252

254253
void EditBoxImplCommon::setText(const char* text)
255254
{
256-
this->setNativeText(text);
257-
_text = text;
258-
refreshInactiveText();
255+
if (nullptr != text) {
256+
this->setNativeText(text);
257+
_text = text;
258+
refreshInactiveText();
259+
}
259260
}
260261

261262
void EditBoxImplCommon::setPlaceHolder(const char* pText)
262263
{
263264
if (pText != NULL)
264265
{
265266
_placeHolder = pText;
266-
_labelPlaceHolder->setString(_placeHolder);
267-
268267
this->setNativePlaceHolder(pText);
268+
_labelPlaceHolder->setString(_placeHolder);
269269
}
270270
}
271271

@@ -308,20 +308,21 @@ void EditBoxImplCommon::openKeyboard()
308308
{
309309
_label->setVisible(false);
310310
_labelPlaceHolder->setVisible(false);
311-
311+
_editingMode = true;
312312
this->setNativeVisible(true);
313313
this->nativeOpenKeyboard();
314314
}
315315

316316
void EditBoxImplCommon::closeKeyboard()
317317
{
318318
this->nativeCloseKeyboard();
319+
_editingMode = false;
319320
}
320321

321322
void EditBoxImplCommon::onEndEditing(const std::string& /*text*/)
322323
{
324+
_editingMode = false;
323325
this->setNativeVisible(false);
324-
325326
refreshInactiveText();
326327
}
327328

cocos/ui/UIEditBox/UIEditBoxImpl-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class CC_GUI_DLL EditBoxImplCommon : public EditBoxImpl
157157

158158
int _maxLength;
159159
Size _contentSize;
160+
bool _editingMode;
160161
};
161162

162163

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ void UIEditBoxTest::editBoxEditingDidEndWithAction(cocos2d::ui::EditBox* editBox
152152
void UIEditBoxTest::editBoxTextChanged(cocos2d::ui::EditBox* editBox, const std::string& text)
153153
{
154154
log("editBox %p TextChanged, text: %s ", editBox, text.c_str());
155+
editBox->setText(text.c_str());
155156
}
156157

157158
void UIEditBoxTest::editBoxReturn(ui::EditBox* editBox)

0 commit comments

Comments
 (0)