简体   繁体   中英

Weird issue with Samsung keyboard on Android with Libgdx

In my game, when a textfield is touched, the view moves up along with the keyboard.

Here's the code in AndroidLauncher:

onCreate(){
//other codes...
setListenerToRootView()
}

private fun setListenerToRootView() {
    val activityRootView: View = window.decorView.findViewById(content)
    activityRootView.viewTreeObserver.addOnGlobalLayoutListener(keyboardLayoutListener)
}

private var keyboardLayoutListener: OnGlobalLayoutListener? = OnGlobalLayoutListener {
    val visibleDisplayFrame = Rect()
    window.decorView.getWindowVisibleDisplayFrame(visibleDisplayFrame)
    sizeChanged(visibleDisplayFrame.width(), visibleDisplayFrame.height())
}

override fun sizeChanged(width: Int, height: Int) {
    val heightRatio = Gdx.graphics.height / main.worldHeight
    val worldHeightChanged = height / heightRatio
    val keyboardStatus = if (height == Gdx.graphics.height) KeyboardStatus.HIDE else KeyboardStatus.SHOW
    main.platformsObservable.notifyObservers(Triple(ObservableKeys.SCREEN_SIZE_CHANGED, keyboardStatus, worldHeightChanged))
    log.error("SCREEN_SIZE_CHANGED, status = $keyboardStatus")
}

The above code gets the keyboard height and status to send to my libgdx game class for the Camera to move the screen up/down.

With a normal keyboard it would send something like this for when the keyboard is shown:

  • SCREEN_SIZE_CHANGED, status = SHOW

and when the keyboard is hidden:

  • SCREEN_SIZE_CHANGED, status = HIDE

But on Samsung devices with the keyboard id of "com.samsung.android.honeyboard/.service.HoneyBoardService" then it does all this when the keyboard is shown once:

  • SCREEN_SIZE_CHANGED, status = HIDE
  • SCREEN_SIZE_CHANGED, status = SHOW
  • SCREEN_SIZE_CHANGED, status = SHOW
  • SCREEN_SIZE_CHANGED, status = SHOW

And this is making the keyboard blocking the textfield in my game because the view doesn't move up.

My gdxVersion is 1.11.0

How can I fix this?

Hi This isn't a libGDX issue. You need to provide parameters to your activity specifically saying what you want for this value (being the screen keyboard as opposed to a hardware one)

android:windowSoftInputMode

as described here https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft and consider what you want for the parameters

adjustResize

adjustPan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM