2012-03-25 82 views
1

我programmagitcallly創建使用此代碼EditView中:如何處理硬件鍵盤輸入視圖?

mSearchBar = new EditText(this); 

    mSearchBar.setHint("Enter sim name."); 

    mSearchBar.setLayoutParams(new LayoutParams(
       layoutWidth, 
       LayoutParams.WRAP_CONTENT)); 

    mSearchBar.setOnEditorActionListener(mEditorAction); 

    mSearchBar.setImeOptions(EditorInfo.IME_ACTION_SEARCH); 

    mSearchBar.setInputType(
       InputType.TYPE_CLASS_TEXT 
      | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS 
      ); 

然後我有mEditorAction與...

private OnEditorActionListener mEditorAction = new OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if(actionId == EditorInfo.IME_ACTION_SEARCH) { 
      gotoSim(v.getText().toString()); 
      InputMethodManager imm = (InputMethodManager)SLMapTest.this 
       .getSystemService(SLMapTest.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      mSearch.collapseActionView(); 
      return true; 
     } 
     return false; 
    } 
}; 

但是這個代碼(這是我從有點谷歌搜索得到)沒有按」用硬件鍵盤玩很好,顯然。我能做些什麼來解決這個問題?

回答

3

連接onKeyListener()到您的視圖,然後在關鍵監聽器監聽

if(keyCode == KeyEvent.KEYCODE_ENTER) //do something