5

好吧,我已经尝试了在stackoverflow和其他地方可以找到的所有解决方案,但最终结果并没有改变一点:触发onKey事件或dispatchKeyEvent的唯一键是虚拟键盘上的回车键,并且只有按下两次。我无法截获任何其他键(事件顶部有一个断点,并且 Eclipse 永远不会在其上停止,除非是上述输入)。模拟器和真实设备上的行为是相同的。有任何想法吗?提前谢谢我的代码 atm 如下所示:

public class ChatWindow {

 @Override
public void onCreate(Bundle savedInstanceState) {
    //...
    setSendText();
            //...
}

    private void setUserInput() {
    Intent i = getIntent();
    mChatInput = (EditText) findViewById(R.id.UsrInput);
    if (i.hasExtra(INTENT_EXTRA_MESSAGE)) {
        mChatInput.setText(i.getExtras().getString(INTENT_EXTRA_MESSAGE));
    }
    mChatInput.setFocusable(true);
    mChatInput.requestFocus();
    mChatInput.setFocusableInTouchMode(true);

    mChatInput.addTextChangedListener(new TextWatcher() {

        public void  afterTextChanged (Editable s){ 
            Log.d(TAG, "afterTextChanged"); 
            if (mChatInput.getText().length() >= 1) {
                mChatInput.addTextChangedListener(this);
                //mChatInput.setOnKeyListener(this);
                //mChatInput.dispatchKeyEvent(this);
                mSendButton.setEnabled(true);       
            }
                    else 
                       mSendButton.setEnabled(false);
            } 

                    public void  beforeTextChanged  (CharSequence s, int start, int 
                    count, int after)
            { 
                    Log.d(TAG, "beforeTextChanged"); 
            } 

                    public void  onTextChanged  (CharSequence s, int start, int before, 
                    int count) 
            { 
                    Log.d(TAG, s.toString()); 
            }


         }); 

}
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
//THIS HERE IS NEVER FIRED UNLESS THE ENTER KEY IS CLICKED TWICE    
    if (event.getAction() == KeyEvent.ACTION_DOWN 
            && event.getKeyCode()==KeyEvent.KEYCODE_ENTER) {
    sendMessageIfNotNull(); 
return true;
}

    return false;
}
4

1 回答 1

0

dispatchKeyEvent 仅在活动具有焦点时才起作用

于 2014-09-02T10:14:58.227 回答