简体   繁体   English

Android 自定义键盘在 BottomSheetDialogFragment 中不起作用

[英]Android Custom Keyboard Not Working in BottomSheetDialogFragment

I created custom keyboard and it works in activity and fragment.我创建了自定义键盘,它适用于活动和片段。

But unfortunately it not working in bottom sheet dialog.但不幸的是它在底部工作表对话框中不起作用。 When i press a key, there is nothing happen to my edit text.当我按下一个键时,我的编辑文本没有任何反应。

Here's my code :这是我的代码:

Key Xml密钥 XML

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="3px"
    android:keyWidth="10%p"
    android:keyHeight="60dp"
    android:keyBackground="@color/colorPrimary"
    android:keyTextColor="@color/colorWhite"
    android:verticalGap="5px">
    <Row>
        <Key
            android:codes="8"
            android:keyEdgeFlags="left"
            android:keyLabel="1" />

        <Key
            android:codes="9"
            android:keyLabel="2" />

        <Key
            android:codes="10"
            android:keyLabel="3" />

        <Key
            android:codes="11"
            android:keyLabel="4" />

        <Key
            android:codes="12"
            android:keyLabel="5" />

        <Key
            android:codes="13"
            android:keyLabel="6" />

        <Key
            android:codes="14"
            android:keyLabel="7" />

        <Key
            android:codes="15"
            android:keyLabel="8" />

        <Key
            android:codes="16"
            android:keyLabel="9" />

        <Key
            android:codes="7"
            android:keyLabel="0" />

        <Key
            android:codes="67"
            android:isRepeatable="true"
            android:keyWidth="10%p"
            android:keyEdgeFlags="right"
            android:keyIcon="@drawable/ic_backspace" />
    </Row>
</Keyboard>

My Keyboard Listener : Below is my keyboard listener code, and i think the problem is targetActivity.dispatchKeyEvent(event) .我的键盘监听器:下面是我的键盘监听器代码,我认为问题是targetActivity.dispatchKeyEvent(event) But i don't get a way to dispatchKeyEvent to a context.但是我没有办法将KeyEvent 分派到上下文。

class BasicOnKeyboardActionListener(
    private val targetActivity: Activity,
    private val keyboardView: KeyboardView
) : KeyboardView.OnKeyboardActionListener {

    override fun swipeRight() {

    }

    override fun onPress(p0: Int) {
    }

    override fun onRelease(p0: Int) {
    }

    override fun swipeLeft() {
    }

    override fun swipeUp() {
    }

    override fun swipeDown() {
    }

    override fun onKey(primaryCode: Int, p1: IntArray?) {
        val eventTime = System.currentTimeMillis()
        val event = KeyEvent(
            eventTime, eventTime, KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0,
            KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE
        )

        targetActivity.dispatchKeyEvent(event)
    }

    override fun onText(p0: CharSequence?) {
    }

    fun registerEditText(edittext: EditText) {
        with(edittext) {
            addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(
                    s: CharSequence,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(
                    s: CharSequence,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                }

                override fun afterTextChanged(editable: Editable) {
                    val mS = editable.subSequence(0, editable.length)
                    if (mS.toString() == "" || mS.toString() == null) {
                        return
                    }

                    if (editable.isNotEmpty() && mS.toString().contains("=")) {
                        editable.replace(editable.length - 1, editable.length, "")
                    }
                }
            })

            onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
                if (hasFocus) showCustomKeyboard(v) else hideCustomKeyboard()
            }

            setOnTouchListener { view, event ->
                val editableText = view as EditText
                val inputType = editableText.inputType

                with(editableText) {
                    setInputType(InputType.TYPE_NULL)
                    onTouchEvent(event)
                    setInputType(inputType)
                }

                true
            }
        }
    }

    private fun hideCustomKeyboard() {
        with(keyboardView) {
            isGone = true
            isEnabled = false
        }
    }

    private fun showCustomKeyboard(view: View?) {
        with(keyboardView) {
            isVisible = true
            isEnabled = true
        }
        view?.let {
            (targetActivity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
                view.windowToken,
                0
            )
        }
    }
}

Implementation Below is my code for my bottomsheetdialogfragment :实现下面是我的bottomsheetdialogfragment 的代码:

    private val keyboardListener by lazy {
        BasicOnKeyboardActionListener(activity ?: requireActivity(), keyboard_view)
    }

    private val customKeyboard by lazy {
        Keyboard(requireContext(), R.xml.keys_layout)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        registerKeyboardEvent()
        setCustomKeyboard()
    }

    private fun setCustomKeyboard() {
        with(keyboard_view) {
            isPreviewEnabled = false
            keyboard = customKeyboard
            setOnKeyboardActionListener(keyboardListener)
        }
    }

    private fun registerKeyboardEvent() {
        with(keyboardListener) {
            registerEditText(et_detailstock_hetnormal)
            registerEditText(et_detailstock_hetpromo)
            registerEditText(et_detailstock_pcs)
            registerEditText(et_detailstock_ctn)
            registerEditText(et_detailstock_tgh)
            registerEditText(et_detailstock_generalpcs)
        }
    }


i fixed this issue by changed this line private val targetActivity: Activity to private val dialog: Dialog .我通过将这一行private val targetActivity: Activity更改为private val dialog: Dialog来解决这个问题。

because the dispatchEvent is always return false in bottom sheet dialog fragment.因为dispatchEvent在底部表单对话框片段中总是返回 false。

reference : Android dispatchKeyEvent not called when Dialog fragment is show参考: 显示对话框片段时未调用 Android dispatchKeyEvent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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