2013-03-15 53 views
0

软键盘在我的活动中不起作用?但在按回家按钮或任何系统用户界面按钮后,除了后退按钮,其开始工作正常。Android onKeyUp在Activity中不起作用

@Override 
public boolean onKeyUp(int keyCode, final KeyEvent event) { 
    final int finalKeyCode = keyCode; 
    View lView = mParent.lET.findFocus(); 
    if(lView == mParent.lET) 
    { 
     if(keyCode == KeyEvent.KEYCODE_ENTER) 
     { 
      this.mGLThread.androidHideSoftKeyboard(); 
     } 
     else 
     { 
      mParent.lET.bringToFront(); 
      mParent.lET.onKeyUp(finalKeyCode, event); 
      mPlayerName = mParent.lET.getText().toString(); 
     } 
    } 

    return false; 
} 

硬件按钮触发此功能,但软键盘没有工作。 谢谢。

+0

它是做正确的任务,为什么抱怨呢..? – Pragnani 2013-03-15 05:51:52

回答

0

onKeyUp应该处理硬键而不是软键。所以你不能像这样处理软键盘的按键。要做到这一点,你可以做一件事。在EditText上设置TextChangedListener。示例代码

edit.addTextChangedListener(new TextWatcher(){ 
      public void afterTextChanged(Editable s) { 

      } 
      public void beforeTextChanged(CharSequence s, int start, int count, int after){ 

      } 
      public void onTextChanged(CharSequence s, int start, int before, int count){ 

      } 

     }); 
+0

这段代码的工作原理与onKeyListener类似:在开始时不工作,但在失去应用程序焦点之后工作 – user2172670 2013-03-15 06:30:41

+0

它实际上并不是任何关键的侦听器。它告诉你文本是否被改变了。据我所知,没有其他方式分别处理软键盘按键。 – stinepike 2013-03-15 06:45:09

0

onKeyListener通过软键盘完美地工作在Android 1.5

在Android 1.6中,字符和数字键都没有通过安其事件去,但DEL键确实

0

给这是一个尝试

设置你定义的监听到你EditText

edittext.setOnEditorActionListener(new HideYourKeypad()); 

定义你的听众

// Added try-catch just in case JellyBean has any other lurking errors 
public class HideYourKeypad implements OnEditorActionListener { 
    @Override 
    public boolean onEditorAction(TextView view, int actionId, 
      KeyEvent event) { 
     try { 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

      if (imm != null && view != null) { 
       switch (actionId) { 
       case EditorInfo.IME_NULL: 
        if ((event == null) 
          || (event.getAction() == KeyEvent.ACTION_DOWN)) 
         imm.hideSoftInputFromWindow(view.getWindowToken(), 
           0); 
        return true; 

       case EditorInfo.IME_ACTION_GO: 
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
        return true; 

       case EditorInfo.IME_ACTION_DONE: 
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
        return true; 
       } 
      } 
     } catch (Throwable x) { 
      Logger.warning(TAG, "Error hiding keyboard", x); 
     } 

     return false; 
    } 
}