android: history edit text

本小妞迷上赌 提交于 2019-12-23 04:08:13

问题



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


回答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.



来源:https://stackoverflow.com/questions/10726384/android-history-edit-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!