安卓:为什么我OnKeyListener()不叫? [英] Android: why is my OnKeyListener() not called?

查看:160
本文介绍了安卓:为什么我OnKeyListener()不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义一个EditText场,我想通知当用户编辑的字段。 因此,我认为:简单 - 我添加OnKeyListener,所以我做到了。但是,即使该文本字段被编辑(甚至显示输入/修改文本)我没有得到任何回调,即LOG输出显示不出来。

I defined an EditText-field and I want to be informed when the user edits that fields. So I thought: simple - I add an OnKeyListener and so I did. But even though the text field gets edited (and even displays the entered/modified text) I don't get any callback, i.e. the LOG-output doesn't show up.

    TextView text = new TextView(this);
    text.setText(...);
    ...
    text.setOnKeyListener(new OnKeyListener()
    {                           
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            TextView tv = (TextView)v;
            CharSequence val = tv.getText();
            Log.v(TAG, "value: " + val);
            // ... rest omitted for brevity
        }
    });

任何想法,为什么回调从不叫什么名字?

Any idea, why that callback is never called?

迈克尔

PS:无语了! Android是真是充满奇特的!看来,几乎没有什么我感动至今曾立刻正如人们所期望的那样。而且 - 信不信由你 - 我有经验的GUI地段,ESP。在Java中(AWT,Swing的,SWT,你的名字......),但Android是一个非常艰难的野兽!

PS.: Sigh! Android is really full of oddities! It seems that almost nothing I touched so far worked immediatly as one would expect. And - believe it or not - I have LOTS of experience with GUIs, esp. in Java (AWT, Swing, SWT, you name it...) But Android is a really tough beast!

推荐答案

在编辑文本输入?我相信,onKeyListener只被使用硬件键盘事件调用。你最好还是使用TextWatcher如果你能。 <一href="http://stackoverflow.com/questions/1967740/onkeylistener-not-working-with-soft-keyboard-android">http://stackoverflow.com/questions/1967740/onkeylistener-not-working-with-soft-keyboard-android

Are you using the soft keyboard (ime) to type in the edit text? I believe that the onKeyListener only gets invoked with events from the hardware keyboard. You are better off using the TextWatcher if you can. http://stackoverflow.com/questions/1967740/onkeylistener-not-working-with-soft-keyboard-android

这篇关于安卓:为什么我OnKeyListener()不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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