Android的安其 - dispatchKeyEvent不点火 [英] Android onkey - dispatchKeyEvent not firing

查看:517
本文介绍了Android的安其 - dispatchKeyEvent不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很好,我已经试过所有解决方案我能找到的计算器和在其他地方,但最终的结果并没有改变一个位:触发安其事件或dispatchKeyEvent是在回车键的唯一关键虚拟键盘,并且仅当pressed两次。我不能拦截任何其他键(有在事件的顶部断点和eclipse永远不会停止它,除了在上述的情况下,进入)。 Beheaviour是两个模拟器和真实设备是一样的。有任何想法吗?先谢谢你
我的code ATM看起来是这样的:

 公共类ChatWindow { @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    // ...
    setSendText();
            // ...
}    私人无效setUserInput(){
    意向I = getIntent();
    mChatInput =(EditText上)findViewById(R.id.UsrInput);
    如果(i.hasExtra(INTENT_EXTRA_MESSAGE)){
        mChatInput.setText(i.getExtras()的getString(INTENT_EXTRA_MESSAGE));
    }
    mChatInput.setFocusable(真);
    mChatInput.requestFocus();
    mChatInput.setFocusableInTouchMode(真);    mChatInput.addTextChangedListener(新TextWatcher(){        公共无效afterTextChanged(编辑S){
            Log.d(TAGafterTextChanged);
            如果(mChatInput.getText()长度()方式> = 1){
                mChatInput.addTextChangedListener(本);
                //mChatInput.setOnKeyListener(this);
                //mChatInput.dispatchKeyEvent(this);
                mSendButton.setEnabled(真);
            }
                    其他
                       mSendButton.setEnabled(假);
            }                    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释
                    算,之后INT)
            {
                    Log.d(TAGbeforeTextChanged);
            }                    公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
                    诠释计数)
            {
                    Log.d(TAG,s.toString());
            }
         });}
}@覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){
//这下面是从来没有发射,除非回车键两次CLICKED
    如果(event.getAction()== KeyEvent.ACTION_DOWN
            &功放;&安培; event.getKey code()== KeyEvent.KEY code_ENTER){
    sendMessageIfNotNull();
返回true;
}    返回false;
}


解决方案

dispatchKeyEvent只有当活动有重点的工作。

well, I've tried all the solutions I could find on stackoverflow and in other places, but the end result hasn't changed one bit: the only key that triggers the onKey event or the dispatchKeyEvent is the enter key on the virtual keyboard, and only if pressed twice. I cannot intercept any other key (there's a breakpoint at the top of the event and eclipse never stops on it, except for in case of the aforementioned enter). Beheaviour is the same on both emulator and real device. Any ideas? Thank you in advance My code atm looks like this:

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 will work only when the activity has focus

这篇关于Android的安其 - dispatchKeyEvent不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆