【问题标题】:Floating Keyboard浮动键盘
【发布时间】:2023-04-07 06:56:01
【问题描述】:

我正在尝试为 Android 创建自定义键盘。

我已经成功创建了一个,但是当我移动和重新定位它时遇到问题,或者让它太高。

如果我移动它来更新 Window LayoutParams 的 x 和 y 坐标,当我向上移动时,聚焦的 EditText 也会向上移动,我需要它来保持它的位置。如果我创建一个不会移动但非常高的键盘,EditText 也会上升。

有什么方法可以解除键盘和与之关联的 EditText 的链接?

相关代码,MyInputIMethodService

import android.inputmethodservice.InputMethodService
import android.view.View
import android.view.inputmethod.InputMethodManager

class MyInputIMethodService : InputMethodService() {

    private lateinit var keyboardDragDelegate: KeyboardDragDelegate
    private lateinit var view: MyKeyboardView
    private lateinit var mInputMethodManager: InputMethodManager

    override fun onCreate() {
        super.onCreate()
        mInputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        keyboardDragDelegate = KeyboardDragDelegate(this, window.window!!)
    }

    override fun isFullscreenMode(): Boolean {
        window?.window?.decorView?.findViewById<View>(android.R.id.content)?.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        return super.isFullscreenMode()
    }

    override fun onEvaluateFullscreenMode(): Boolean {
        return false
    }

    private val navBarHeight: Int
        get() {
            val resources = resources
            val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            return if (resourceId > 0) {
                resources.getDimensionPixelSize(resourceId)
            } else 0
        }

    override fun onComputeInsets(outInsets: Insets) {
        outInsets.contentTopInsets = view.height + navBarHeight + (window.window?.attributes?.y ?: 0)
    }

    override fun onCreateInputView(): View {
        view = MyKeyboardView(context = this).apply {
            //button listeners here...
        }
        return view
    }
}

我也尝试过让键盘全屏透明,但是后面的 UI 无法交互。

我知道这是可能的,因为至少 Siwftkey 做到了。

有人可以帮我吗?

谢谢

【问题讨论】:

    标签: android keyboard floating


    【解决方案1】:
    @Override
    public void onComputeInsets(Insets outInsets) {
        super.onComputeInsets(outInsets);
        outInsets.visibleTopInsets = -1000;
    }
    

    【讨论】:

    • 请不要只发布代码作为答案。请解释您的答案/实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2019-10-29
    • 2019-11-26
    • 1970-01-01
    • 2020-08-11
    相关资源
    最近更新 更多