Android EditText上的setOnKeyListener不工作

Android EditText上的setOnKeyListener不工作,android,android-edittext,Android,Android Edittext,我已经在活动中对editText应用了setOnKeyListener,但当我按下键盘上的任何键时,它不会调用此方法 我想检测键盘上是否按了空格键或enter键,但我没有找到任何方法来找出这些键代码 我也试过TextWatcher,但它没有告诉我们我想要的键码 如果有人知道我们怎么做,请帮助我 非常感谢您的帮助。您可以检测到按下的Enter键,如下所示 editText.setOnKeyListener(new View.OnKeyListener() { public

我已经在活动中对editText应用了setOnKeyListener,但当我按下键盘上的任何键时,它不会调用此方法

我想检测键盘上是否按了空格键或enter键,但我没有找到任何方法来找出这些键代码

我也试过TextWatcher,但它没有告诉我们我想要的键码

如果有人知道我们怎么做,请帮助我


非常感谢您的帮助。

您可以检测到按下的Enter键,如下所示

editText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                            (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    Log.e("Value", "Enter");
                    return true;
                }
                return false;


            }
        });

请粘贴您的代码片段?另外,在你的TextWatcher中引用从EditText获取文本,然后检查字符串是否有空格。我对此投反对票,因为他已经提到了使用TextWatcher。
//You should try 

edittext..addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });