I'm beginning android development and was going through some guides in which I tumbled across this piece of code :

 myEditText.setOnKeyListener (new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN)
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
        {
          todoItems.add(0, myEditText.getText().toString());
          aa.notifyDataSetChanged();
          myEditText.setText(“”);
          return true;
        }
      return false;
    }
  });

I would like to ask, what kind of parameter is the method "myEditText.setOnKeyListener" taking? It looks confusing to me because it contains new keyword, bunch of codes in curly braces, return...etc etc.... Anyone please explain.....

regards,

Dani AI

Generated

Short answer: setOnKeyListener takes an instance of the View.OnKeyListener interface. What you saw is an anonymous inner class: new OnKeyListener() { ... } defines and instantiates a one-off class that implements that interface and overrides its onKey(...) method. That was the quick pointer suggested — search for “anonymous inner class” for more examples.

The method signature looks like this:

public void setOnKeyListener(android.view.View.OnKeyListener l)

The anonymous inner class creates an unnamed object that implements the interface and is passed straight into the setter. Modern Android projects that enable Java 8 language features (desugaring) can use a lambda instead, since OnKeyListener is a single-abstract-method interface:

myEditText.setOnKeyListener((v, keyCode, event) -> {
  // handle key, return true to consume
  return false;
});

Practical notes and gotchas: returning true from onKey consumes the event; returning false lets it propagate. Many soft-keyboard (IME) actions do NOT send key events to OnKeyListener; to capture IME actions like “Done” or “Enter” reliably use TextView.OnEditorActionListener instead (see Android docs linked below). Also remember older Java required captured local variables used inside anonymous classes to be final (Java 8 relaxed this to “effectively final”).

Extra caution: anonymous inner classes hold an implicit reference to their outer instance. That’s normally fine for short-lived UI listeners, but be careful if you attach long-lived listeners or launch background work from them to avoid leaking Activities. For background reading, see the Java anonymous-class tutorial and the Android docs on View.OnKeyListener and Java 8 support:

This expands on the brief thread: ’s snippet is an anonymous inner-class instance implementing OnKeyListener, and ’s suggestion to search for anonymous inner classes is the right next step.

Recommended Answers

All 2 Replies

Google: anonymous inner class java

Ok, Got it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.