Android:为什么我的 OnKeyListener() 没有被调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3697063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:25:35  来源:igfitidea点击:

Android: why is my OnKeyListener() not called?

android

提问by mmo

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.

我定义了一个 EditText 字段,我希望在用户编辑该字段时收到通知。所以我想:简单 - 我添加了一个 OnKeyListener ,所以我做了。但即使文本字段被编辑(甚至显示输入/修改的文本),我也没有收到任何回调,即日志输出不显示。

    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?

任何想法,为什么从不调用该回调?

Michael

迈克尔

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!

PS:叹气!Android真的充满了古怪!到目前为止,我所接触的几乎所有东西似乎都没有像人们预期的那样立即起作用。而且 - 信不信由你 - 我在 GUI 方面有很多经验,尤其是。在 Java 中(AWT、Swing、SWT,你能说出它的名字……)但 Android 是一个非常强悍的野兽!

采纳答案by Cheryl Simon

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. onKeyListener not working with soft keyboard (Android)

您是否使用软键盘 (ime) 输入编辑文本?我相信 onKeyListener 只会被来自硬件键盘的事件调用。如果可以,最好使用 TextWatcher。 onKeyListener 不适用于软键盘(Android)

回答by Kaptkaos

I had the exact same problem, but on only 1 of my Android apps and I never did figure out what the difference was.

我遇到了完全相同的问题,但只在我的 1 个 Android 应用程序上出现过,我从来没有弄清楚有什么区别。

My solution though was to do what Mayra suggested and add a TextWatcher to handle the TextChanged events. So it works no matter how the text entry occurs.

不过,我的解决方案是按照 Mayra 的建议进行操作,并添加一个 TextWatcher 来处理 TextChanged 事件。因此,无论文本输入如何发生,它都有效。

editName.addTextChangedListener(new TextWatcher () {
        public void afterTextChanged(Editable s) {
            Button btnSave = (Button)findViewById(R.id.btnSaveToon);
            if(s.length() > 0)
                btnSave.setEnabled(true);
            else
                btnSave.setEnabled(false);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

    });

Works like a charm in the emulator and on my HTC Inspire

在模拟器和我的 HTC Inspire 上工作就像一个魅力

回答by MatrixFrog

You say that you're dealing with an EditText, but your code refers to a TextView. My guess is that you have an EditText in your layout XML files, but you're referring to this newly created TextView in your code, which is in fact not even in the app's UI at all.

你说你正在处理一个 EditText,但你的代码引用了一个 TextView。我的猜测是您的布局 XML 文件中有一个 EditText,但您在代码中指的是这个新创建的 TextView,实际上它甚至根本不在应用程序的 UI 中。

If there is already an EditText in your layout XML file, then you need to get a pointer to it in your Java code, probably using the findViewById()method. Then add your OnKeyListener to that EditText.

如果您的布局 XML 文件中已经有 EditText,那么您需要在 Java 代码中获取指向它的指针,可能使用该findViewById()方法。然后将您的 OnKeyListener 添加到该 EditText。

Defining your layout in XML actually makes a lot more sense (at least in many, if not most, cases) than defining it one component at a time and then adding each those components to the UI, like you do in Swing. But it takes some getting used to, no question.

用 XML 定义布局实际上比一次定义一个组件然后将每个组件添加到 UI 更有意义(至少在许多情况下,如果不是大多数情况下),就像在 Swing 中所做的那样。但毫无疑问,这需要一些时间来适应。