1

First summary:

class A extends AutoCompleteTextView {
   public A() {
      setOnKeyListner( ... new OnKeyLisnter() ... ); //listener 1
   }
}
A obj;
obj.setOnKeyListener( ... new OnKeyListner() ... ); //listner 2

Is it possible to have both listeners called ? Right now I get messages only from listener 2.


I am trying to build an edit text with history derived from AutoCompleteTextView . The class has an ArrayAdapter that updates itself whenever an enter key is pressed down. So far so good. But when the object created from this class wants to do more than adding the text to a database, I need to use another onKeyListener. If it does that, the original listener will not be called. Is there a way to let both be called when enter key is pressed down?

class HistoryEditText extends AutoCompleteTextView {
    ArrayAdapter<String> adapter;
    public HistoryEditText(Context ctx) {
        super(ctx);
        setMaxLines(1);
        setOnKeyListener(new aCommand());
        List<String> myList = new LinkedList<String>();
        adapter = new ArrayAdapter<String>(ctx,R.layout.lvtext, myList);
        setAdapter(adapter);
    }
    private class aCommand implements OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                String str = getText().toString();
                setText(" ");
                adapter.add(str);
                adapter.notifyDataSetChanged();
                return true;
            }
            return false;
        }
    }
}

Somewhere else:

   HistoryEditText cmdField = new HistoryEditText(ctx);
   cmdField.setOnKeyListener(... another listener ... )

I tried returning false in one of the listners but didn't work. Also I don't want to use interfaces to merge the two listeners into one. Thanks

danny
  • 1,101
  • 1
  • 12
  • 34

1 Answers1

1

Similar question here might be helpful. Basically the summary is no, you can't.

Android - Two onClick listeners and one button

He uses some dirty hacks to work around it, but he pretty much does exactly what you don't want to do.

Community
  • 1
  • 1
Woody
  • 363
  • 2
  • 14
  • Thank you. I assumed it was possible to register two listeners like in swing but that seems not to be the case. I will probably use one listener rather than implementing an interface. – danny May 23 '12 at 20:56