开发者

Android: KeyListener for an EditText not receiving keys

I have an EditText that I want to monitor KeyEvents for, and I have a listener set up as fo开发者_StackOverflow社区llows:

mText = (EditText) this.findViewById(R.id.title);
mText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        final int view = v.getId();
        switch (view) {
            case R.id.title:
                Log.d(LOG_TAG, "key handled");
                break;
        }
        return false;
    }
});

My problem is that when the EditText is being typed into using the virtual keyboard, the only key press that triggers the logging is the backspace key. I've verified that all other keypresses aren't even triggering onKey(). I'm sure this is something simple, but didn't find anything on SO that seemed to deal with this.

Thanks,

Paul


Try using addTextChangedListener(TextWatcher watcher) defined here with it you can handle the physical and the soft keyboard. I hope it helps


From the Android reference at:

http://developer.android.com/reference/android/view/View.OnKeyListener.html

View.OnKeyListener

Class Overview Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

It seems OnKeyListener is designed expressly to react to HARDWARE keys only!


onKeyListener only works with:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Set it in the onCreate method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜