I've made a custom in-app keyboard (by following this tutorial). But I'd like it to delete text from a different edit text when the delete key is pressed. Is this possible to achieve?
This is my onClick code in my keyboard activity file:
override fun onClick(v: View) {
if (v.id == R.id.button_delete) {
// Delete key is pressed.
} else {
// Regular key is pressed.
val value = keyValues[v.id]
inputConnection?.commitText(value, 1)
}
}
Let me know if there's anything else you'd like to know :)
CodePudding user response:
I eventually solved this by putting a character e.g: 'd' into the edittext like so:
override fun onClick(v: View) {
if (v.id == R.id.button_delete) {
inputConnection?.commitText("d", 1)
} else {
val value = keyValues[v.id]
inputConnection?.commitText(value, 1)
}
}
Then in the activity that controlls the edittext's layout, e.g: for me it was PasscodeActivity
, put a TextWatcher
that detects when the text inside of the edittext has changed.
private fun setEditTextListener() {
val inputEl = findViewById<EditText>(resources.getIdentifier("inputEl", "id", packageName))
inputEl.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(textBeforeChangedStr: CharSequence?, startNum: Int, countNum: Int, afterNum: Int) {
...
}
override fun onTextChanged(textAfterChangedStr: CharSequence?, startNum: Int, beforeNum: Int, countNum: Int) {
...
}
override fun afterTextChanged(editableEl: Editable?) {
if (inputEl.text.toString() == "d") { // The 'd' character is put into the edittext when the delete key is pressed.
clearOtherEditText()
}
...
}
})
}
Then in the function clearOtherEditText()
I put in some code to get rid of the text in a different edittext like so:
val otherInputEl = findViewById<EditText>(resources.getIdentifier("otherInput", "id", packageName))
otherInputEl.text.clear()
Hope this helps someone else :)
CodePudding user response:
try below solution
override fun onClick(v: View) {
if (v.id == R.id.button_delete) {
// Delete key is pressed.
**inputConnection.deleteSurroundingText(1, 0)**
} else {
// Regular key is pressed.
val value = keyValues[v.id]
inputConnection?.commitText(value, 1)
}
}