时才会工作
好吧,我已经在stackerflow和其他地方尝试了我能找到的所有解决方案,但最终结果一点也没有改变:触发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;
}
dispatchKeyEvent只有在活动具有焦点