I have an EditText
View which is edited by setText()
from my code and by the user via (soft/hard) keyboard and - if possible by speech input. I want to handle input made by my code in another way than normal user input: if a user input happens, a internal event shall be fired. But I don't know how to differentiate between both. Previously - when developing on the emulator - I used the onKeyDown()
method to catch user input. However, when testing on a real device, I found out, that onKeyDown()
isn't triggered from soft keyboard input. Additionally speech input wouldn't be recognized by this, though I consider this to be a minor flaw. So that solution is no option for me.
On the other hand there is the onTextChanged()
method, but this is triggered by both setText()
and keyboard input. So how can I differentiate between both or which method is only called by user input, but not when using setText()
, so can I overwrite it?
public class MyTextWatcher implements TextWatcher { private EditText editText; // Pass the EditText instance to TextWatcher by constructor public MyTextWatcher(EditText editText) { this. editText = editText; } @Override public void afterTextChanged(Editable e) { // Unregister self before update editText.
Every text field expects a certain type of text input, such as an email address, phone number, or just plain text. So it's important that you specify the input type for each text field in your app so the system displays the appropriate soft input method (such as an on-screen keyboard).
In android, EditText is a user interface control which is used to allow the user to enter or modify the text. While using EditText control in our android applications, we need to specify the type of data the text field can accept using the inputType attribute.
I finally solved the problem by implementing a InputConnectionWrapper
(see this question and particularly the answer for an implementation example) which has various methods to get the input from a soft-keyboard. I return my InputConnectionWrapper
in the method EditText.onCreateInputConnection()
. For hard keyboards I use EditText.onPreIme()
. All these methods are overwritten and route their input through my framework which handles the text input and updates the View accordingly. This means, in all these overwritten methods (except for onCreateInputConnection()
) the super method is not called cause I update the View myself. This prevents inconsistencies between my data model and the View.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With