2010-07-17 10 views
7

voglio ignorare il comportamento del ENTER tasto della tastiera virtuale in modo che:Come ignorare il comportamento chiave <ENTER> della tastiera virtuale in Android

  • quando ci sono più campi sullo schermo, e 'schede' al prossimo campo
  • quando è l'ultimo campo dello schermo, si esegue l'azione predefinita dello schermo

ho giocato con le opzioni IME e le etichette, ma solo don ottenere quello che voglio. Qualcuno ha qualche suggerimento?

risposta

9

Con l'aiuto su un altro forum, ho trovato il modo di farlo.

Per renderlo riutilizzabile, ho creato la mia classe di dialogo eccellente che contiene 2 OnKeyListener oggetti e un abstract metodo di invio:

public abstract class MyAbstractDialog extends Dialog { 

/** 
* OnKeyListener that puts the focus down when the ENTER key is pressed 
*/ 
protected View.OnKeyListener onEnterFocusDown = new View.OnKeyListener() { 

       @Override 
       public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
          (keyCode == KeyEvent.KEYCODE_ENTER)) { 
           v.requestFocus(View.FOCUS_DOWN); 
         return true; 
       } 
         return false; 
       } 
     }; 

/** 
* OnKeyListener that submits the page when the ENTER key is pressed 
*/ 
protected View.OnKeyListener onEnterSubmitView = new View.OnKeyListener() { 

       @Override 
       public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
          (keyCode == KeyEvent.KEYCODE_ENTER)) { 
           submitView(v); 
         return true; 
       } 
         return false; 
       } 
     }; 
     protected abstract void submitView(View v); 

} 

Ora, nella Dialog posso utilizzare questi oggetti per impostare i miei campi:

// make the ENTER key on passwordField1 put the focus on the next field 
passwordField1.setOnKeyListener(onEnterFocusDown); 

// make the ENTER key on passwordField2 submit the page 
passwordField2.setOnKeyListener(onEnterSubmitView); 
+0

scusa per il layout scadente, non so perché non ha funzionato nel modo in cui volevo – Aak

Problemi correlati