【问题标题】:InputConnection comitting text deletes the default keyboards input textInputConnection committing text 删除默认键盘输入文本
【发布时间】:2020-05-01 11:52:46
【问题描述】:

大家好,我正在尝试制作一个应用内键盘,它正在工作,除非默认键盘输入了一些内容,然后我的键盘输入了一些内容,默认键盘输入在我的键盘输入输入之前被删除。

class MyKeyboard @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr), View.OnClickListener {

    private var button1: Button? = null
    private var button2: Button? = null
    private var button3: Button? = null
    private var button4: Button? = null
    private var button5: Button? = null
    private var button6: Button? = null
    private var button7: Button? = null
    private var button8: Button? = null
    private var button9: Button? = null
    private var button0: Button? = null
    private var buttonDelete: Button? = null
    private var buttonEnter: Button? = null

    private val keyValues = SparseArray<String>()
    private var inputConnection: InputConnection? = null

    init {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        LayoutInflater.from(context).inflate(R.layout.keyboard, this, true)
        button1 = findViewById(R.id.button_1) as Button
        button1!!.setOnClickListener(this)
        button2 = findViewById(R.id.button_2) as Button
        button2!!.setOnClickListener(this)
        button3 = findViewById(R.id.button_3) as Button
        button3!!.setOnClickListener(this)
        button4 = findViewById(R.id.button_4) as Button
        button4!!.setOnClickListener(this)
        button5 = findViewById(R.id.button_5) as Button
        button5!!.setOnClickListener(this)
        button6 = findViewById(R.id.button_6) as Button
        button6!!.setOnClickListener(this)
        button7 = findViewById(R.id.button_7) as Button
        button7!!.setOnClickListener(this)
        button8 = findViewById(R.id.button_8) as Button
        button8!!.setOnClickListener(this)
        button9 = findViewById(R.id.button_9) as Button
        button9!!.setOnClickListener(this)
        button0 = findViewById(R.id.button_0) as Button
        button0!!.setOnClickListener(this)
        buttonDelete = findViewById(R.id.button_delete) as Button
        buttonDelete!!.setOnClickListener(this)
        buttonEnter = findViewById(R.id.button_enter) as Button
        buttonEnter!!.setOnClickListener(this)

        keyValues.put(R.id.button_1, "1")
        keyValues.put(R.id.button_2, "2")
        keyValues.put(R.id.button_3, "3")
        keyValues.put(R.id.button_4, "4")
        keyValues.put(R.id.button_5, "5")
        keyValues.put(R.id.button_6, "6")
        keyValues.put(R.id.button_7, "7")
        keyValues.put(R.id.button_8, "8")
        keyValues.put(R.id.button_9, "9")
        keyValues.put(R.id.button_0, "0")
        keyValues.put(R.id.button_enter, "\n")
    }

    override fun onClick(view: View) {
        if (inputConnection == null)
            return

        if (view.id == R.id.button_delete) {
            val selectedText = inputConnection!!.getSelectedText(0)

            if (TextUtils.isEmpty(selectedText)) {
                inputConnection!!.deleteSurroundingText(1, 0)
            } else {
                inputConnection!!.commitText("", 1)
            }
        } else {
            val value = keyValues.get(view.id)

            inputConnection!!.commitText(value, 1)


        }
    }

    fun setInputConnection(ic: InputConnection) {
        inputConnection = ic
    }
}

所以我想在button press 上拥有default keyboardpops uppops up 系统。就像我说的那样,除了我在系统键盘上输入内容然后在 inapp 自定义键盘上输入内容时,系统键盘文本被删除,自定义键盘 text is entered 位于该行的 beginning 上。每行都会发生这种情况

【问题讨论】:

  • 你找到解决办法了吗。

标签: android kotlin android-softkeyboard inputconnection


【解决方案1】:

在执行任何其他操作之前调用 inputConnection.finishComposingText。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多