大家好,我正在尝试制作一个应用内键盘,它正在工作,除非默认键盘输入了一些东西然后我的键盘输入了一些东西,默认键盘输入在我的键盘输入被输入之前被删除。
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 自定义键盘上输入内容时,一切正常,系统键盘文本被删除,自定义键盘位于行的末尾。这发生在每行inapp keyboard
pops up
button press
text is entered
beginning