2012-10-14 10 views
9

Sto lavorando su Android. Precedentemente ho usato onKeyListener per gestire un'azione specifica sull'evento chiave.Come gestire il tasto Invio tramite TextWatcher su Android

Tuttavia, questo modo sembra non risolvere il mio problema poiché quasi tutte le chiavi si disabiliterebbero una volta implementato quell'ascoltatore sul mio EditText. Dopo aver letto alcuni argomenti in SO, so che invece dovrei usare TextWatcher, ma mi sto ancora chiedendo come gestire l'evento chiave ENTER all'interno perché i parametri forniti sono solo CharSequence, Editable, ecc. Non ho trovato alcun parametro keyCode.

+0

'Tuttavia, questo modo sembra non risolvere il mio problema poiché quasi tutte le chiavi si disabiliterebbero una volta che ho implementato quel listener sul mio EditText. Sembra che il tuo codice non sia corretto. Dovrebbe funzionare. con 'watcher del testo 'non puoi ottenere' Inserisci evento' –

+1

perché la mia domanda non è stata votata? – mrkhv

risposta

7

provare questo

protected View.OnKeyListener onEnter = new View.OnKeyListener() { 

    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if ((event.getAction() == KeyEvent.ACTION_DOWN) 
       && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
      //here do what you want 
     } 
     return false; // very important 
    } 
}; 
+3

Secondo il doc: _Called quando una chiave hardware viene inviata a una vista._ – Divers

+2

Come ha detto Divers, KeyListener in realtà ascolta i tasti hardware (volume, casa, ecc.). – Waclock

14

provare questo

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n")) { 
     //enter pressed 
    } 
} 
+0

user2348959 grazie per la risposta super solution –

+0

@ user2348959 quel metodo non è adatto allo scopo. Se ti muovi su una linea con back space viene chiamato anche questo. –

+0

@ user2348959 funziona se una nuova riga appare alla fine della stringa .. ma non funziona, viene visualizzata una nuova riga tra la stringa – Pallavi

0

Penso che questa sia la soluzione migliore, perché è possibile premere il tasto 'Invio' in qualsiasi luogo del campo EditText e non solo alla fine di linea.

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

     public void beforeTextChanged(CharSequence s, int st, int ct, 
             int af) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if (s.length() < 1 || start >= s.length() || start < 0) 
       return; 

      // If it was Enter 
      if (s.subSequence(start, start + 1).toString().equalsIgnoreCase("\n")) { 

       // Change text to show without '\n' 
       String s_text = start > 0 ? s.subSequence(0, start).toString() : ""; 
       s_text += start < s.length() ? s.subSequence(start + 1, s.length()).toString() : ""; 
       edittext.setText(s_text); 

       // Move cursor to the end of the line 
       edittext.setSelection(s_text.length()); 
      } 
     } 
    });