好吧,我已经尝试了我在stackoverflow和其他地方找到的所有解决方案,但最终结果没有改变一点:触发onKey事件的唯一键或dispatchKeyEvent是输入键虚拟键盘,只有按两次。我无法拦截任何其他键(事件顶部有一个断点,eclipse永远不会停在它上面,除非是上述输入)。 Beheaviour在仿真器和真实设备上都是相同的。有任何想法吗?先感谢您 我的代码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;
}
答案 0 :(得分:1)
dispatchKeyEvent仅在活动具有焦点时才起作用