InputConnection comiting text удаляет вводимый по умолчанию текст с клавиатуры - PullRequest
1 голос
/ 14 января 2020

Привет всем, я пытаюсь создать встроенную клавиатуру приложения, и она работает, за исключением случаев, когда клавиатура по умолчанию вводит что-то, а затем моя клавиатура вводит что-то, ввод с клавиатуры по умолчанию стирается перед вводом ввода с клавиатуры.

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
    }
}

, поэтому я пытаюсь получить систему default keyboard и inapp keyboard, которые pops up на button press. Как я уже говорил, все работает, за исключением того, что когда я что-то ввожу на системную клавиатуру, а затем вводю что-то на пользовательской клавиатуре inapp, текст на системной клавиатуре удаляется, а пользовательская клавиатура text is entered в beginning строки. это происходит в строке

...