机器人:历史编辑文本 [英] android: history edit text

查看:110
本文介绍了机器人:历史编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先总结:

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

是否有可能同时拥有听众叫什么名字?现在,我只能从监听器2获取消息。

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

我想建立与AutoCompleteTextView衍生历史上编辑文本。这个类有自我更新每当进入关键是pressed下来的ArrayAdapter。到现在为止还挺好。但是,当从这个类创建对象要做到比添加文本数据库的更多,我需要使用另一个onKeyListener。如果确实如此,原始听者将不被调用。有没有办法让时输入关键是pressed下来都被称为?

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;
        }
    }
}

别的地方:

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

我试图在listners之一返回false,但没有奏效。此外,我不希望使用接口的两个监听器合并成一个。
谢谢

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

推荐答案

下面类似的问题可能会有所帮助。基本上总结是否定的,你不能。

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

<一个href=\"http://stackoverflow.com/questions/4618929/android-two-onclick-listeners-and-one-button\">Android - 两个听众的onClick和一键

他使用一些诡异的技巧来解决,但他pretty多不正是你不想做的事。

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

这篇关于机器人:历史编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆