2013-03-15 50 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; 
    } 
} 
相關問題