2012-03-25 6 views
1

Je crée programmagitcallly un EditView avec ce code:Comment gérer le clavier matériel entrer pour une vue?

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 
      ); 

Ensuite, je dois le mEditorAction avec ...

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; 
    } 
}; 

Cependant ce code (que je suis d'un peu de googler) n » t jouer sympa avec les claviers hardware, apparemment. Que puis-je faire pour résoudre ce problème?

Répondre

3

attach onKeyListener() à votre vue, puis en écoute

dans auditeur clé if(keyCode == KeyEvent.KEYCODE_ENTER) //do something

Questions connexes