Ron Gross
Ron Gross

Reputation: 1624

Android EditText OnKeyDown event in xml

Is there any way to assign the OnKeyDown event to an EditText in the xml? I know how to do it in the code, but XML?

(actually i wan't to capture the 'enter' press... so maybe there is an easier way?)

Thank you, Ron

Upvotes: 0

Views: 1876

Answers (2)

Rafael T
Rafael T

Reputation: 15689

as far as I know: No, there isn't an 'easy' way to Capture it in XML. You can capture onClick in xml only. So if you MUST to have it in XML you can do the following:

  1. Extend EditText with your custom class.

then

1.2. Define your custom stylable attributes on it (ie "onKeyDown"). Look here how to do it. I suggest you use a string as value.

1.3. Now the tricky part: You have to use reflection to find the onKeyListener in the activity and add it as KeyListener to your custom editText

OR

2.2 simply let your Activity implement onKeyListener and cast given Context in Constructor of your custom EditText

Upvotes: 2

Raz
Raz

Reputation: 9058

You can use the android:imeOptions in xml to select how the button should behave.

And you can override the button like this:

editText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            /// Do stuff here
        }
    });

Upvotes: 1

Related Questions