Apply generic onkeylistener

New for programming, now for android. Therefore, I hope that I will not greatly annoy you.

How can I configure the onkeylistener setting at the top level of the application that captured the keyevent no matter what.

Basically, I have a linear layout with dynamically added edittext. I want to capture the key event Enter and get it in the current editor, run some tests, then create a new edittext and add it to the layout.

I know that I can (and have) implement an onkeylistener for individual child views, but without being a programmer, the logic seems strange to create an edittext that listens for input to create another edittext that listens for input to create another ... (you see where it happens)

Can someone point me in the right direction? I have more information about what I'm trying to do, I just don’t know what is appropriate and what is not, so let me know if you need more.

Thanks for your time in advance, Chris

+4
source share
3 answers

Take a look at http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

You want to intercept all events before they are processed by any view in the window. Returns true if the event was processed, or false if you want children to process the event further.

+1
source

Like Ben said that your activity can implement OnKeyListener, then for each created EditText set OnKeyListener as activity.

editText1.setOnKeyListener(this); 

And then in your onKey implementation, you can handle the key press event.

 @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(v == editText1) { // do something } else if( v == editText2 ) { // do something } return true; // return true if you handled the keypress } 
0
source

In your activity you can implement OnKeyListener.

-1
source

All Articles