Skip to content

Commit 0b5922b

Browse files
authored
Fix the issue where the input field position is incorrect after the keyboard pops up on an Android pad. (#18984)
* Fix the issue where the input field position is incorrect after the keyboard pops up on an Android pad. * Apply CocosActivity's orientation to CocosEditBoxActivity
1 parent b60d358 commit 0b5922b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

native/cocos/platform/android/java/src/com/cocos/lib/CocosEditBoxActivity.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class CocosEditBoxActivity extends Activity {
6262
// a color of dark green, was used for confirm button background
6363
private static final int DARK_GREEN = Color.parseColor("#1fa014");
6464
private static final int DARK_GREEN_PRESS = Color.parseColor("#008e26");
65+
private static final String ORIENTATION = "orientation";
6566

6667
private static CocosEditBoxActivity sThis = null;
6768
private Cocos2dxEditText mEditText = null;
@@ -70,6 +71,7 @@ public class CocosEditBoxActivity extends Activity {
7071
private boolean mConfirmHold = true;
7172
private int mEditTextID = 1;
7273
private int mButtonLayoutID = 2;
74+
private int mInitialSystemWindowInsetBottom = -1;
7375

7476
/***************************************************************************************
7577
Inner class.
@@ -208,13 +210,13 @@ private void removeListeners() {
208210
this.removeTextChangedListener(mTextWatcher);
209211
}
210212

211-
private boolean isSystemAdjustUIWhenPopKeyboard(int bottom) {
213+
private boolean isSystemAdjustUIWhenPopKeyboard() {
212214
int bottomOffset = 0;
213215
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
214216
bottomOffset = getWindow().getDecorView().getRootWindowInsets().getSystemWindowInsetBottom();
215217
}
216218
// view will be scrolled to the target position by system,
217-
if (Math.abs(bottom - bottomOffset) < 10) {
219+
if (Math.abs(mInitialSystemWindowInsetBottom - bottomOffset) > 100) {
218220
return true;
219221
}
220222
return false;
@@ -232,7 +234,7 @@ public void onGlobalLayout() {
232234
if (!keyboardVisible) {
233235
keyboardVisible = true;
234236
}
235-
if (!isSystemAdjustUIWhenPopKeyboard(heightDiff)) {
237+
if (!isSystemAdjustUIWhenPopKeyboard()) {
236238
getRootView().scrollTo(0, heightDiff);
237239
}
238240
} else {
@@ -287,6 +289,8 @@ public void onClick(View view) {
287289
"text"
288290
);
289291
} else {
292+
int orientation = extras.getInt(ORIENTATION);
293+
setRequestedOrientation(orientation);
290294
show(extras.getString("defaultValue"),
291295
extras.getInt("maxLength"),
292296
extras.getBoolean("isMultiline"),
@@ -296,6 +300,14 @@ public void onClick(View view) {
296300
}
297301
}
298302

303+
@Override
304+
public void onAttachedToWindow() {
305+
View decorView = getWindow().getDecorView();
306+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
307+
mInitialSystemWindowInsetBottom = decorView.getRootWindowInsets().getSystemWindowInsetBottom();
308+
}
309+
}
310+
299311
/***************************************************************************************
300312
Public functions.
301313
**************************************************************************************/
@@ -434,6 +446,7 @@ public void run() {
434446
i.putExtra("confirmHold", confirmHold);
435447
i.putExtra("confirmType", confirmType);
436448
i.putExtra("inputType", inputType);
449+
i.putExtra(ORIENTATION, GlobalObject.getActivity().getRequestedOrientation());
437450
GlobalObject.getActivity().startActivity(i);
438451
}
439452
});

0 commit comments

Comments
 (0)