2

Hello everyone I am trying to make an in app keyboard and it is working except for if the default keyboard inputs something and then my keyboard enters something, the default keyboards input is erased before my keyboard input is put in.

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

so I am trying to have the system default keyboard and an inapp keyboard that pops up on a button press. Like I said everything works except for when I input something on the system keyboard and then enter something on the inapp custom keyboard, the system keyboard text is deleted and the custom keyboard text is entered at the beginning of the line. this happens per line

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
JRowan
  • 6,824
  • 8
  • 40
  • 59

1 Answers1

0

Call inputConnection.finishComposingText before doing anything else.

j__m
  • 9,392
  • 1
  • 32
  • 56