Question

I know how to get KeyEvents from a physical keyboard, but how do I get them from a soft keyboard?

This code does not work for a soft keyboard:

editText.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Toast.makeText(this, Integer.toString(keyCode), Toast.LENGTH_SHORT)
        return false;
    }
});
Was it helpful?

Solution

maybe you need the TextChangedListener (or use it as alternative method):

TextWatcher textWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before,int count) {        
    }
    public void afterTextChanged(Editable s) {                   
    }       
};
editText.addTextChangedListener(textWatcher);

OTHER TIPS

Try this

public void onCreate(Bundle savedInstanceState) {
    ...
    etField =(EditText)findViewById(R.id.your_edit_text);

    etField.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
             // register the text when "enter" is pressed
             if (event.getAction() == KeyEvent.ACTION_DOWN 
             || event.getAction() == KeyEvent.ACTION_UP ) {
             int timesPressed = event.getRepeatCount();
             //do something
        }
    });
}

OR

EditText.addTextChangedListener(new TextWatcher(){
   //do something
}) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top