1

Hello I just started in Xamarin with an Android project and I encountered a problem on the keypress of an edittext. When the Enter is fired on my first Edittext and it gives focus to the Edittext (txtArtikel), but then the keypress of this Edittext is triggered and jumps to the third Edittext. So it skips my second Edittext with just one Enter press. Can anyone help me?

 txtArtikel.KeyPress += txtArtikelPress;

 private void txtArtikelPress(object sender, View.KeyEventArgs e)
 {
     e.Handled = false;
     if (e.KeyCode == Keycode.Enter)
     {
         txtAantal.RequestFocus();
     }
 }

EDIT

I'm still working with the Keypress and now looking for an Enter and Escape. When the txtWerf is empty it jumps to txtArtikel, but when it contains text it goes to txtAantal.

 private void txtWerfPress(object sender, View.KeyEventArgs e)
 {
     if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Down)
     {
         if (txtWerf.Text.Trim() != "")
         {
             if (txtArtikel.RequestFocus())
                 e.Handled = true;
         }
     }
     else
         e.Handled = false;
 }

 private void txtArtikelPress(object sender, View.KeyEventArgs e)
 {
     if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Down)
     {
         if (txtArtikel.Text.Trim() != "")
         {
             if (txtAantal.RequestFocus())
                 e.Handled = true;
         }
     }
     else if (e.KeyCode == Keycode.Escape && e.Event.Action == KeyEventActions.Down)
     {
         if (txtWerf.RequestFocus())
             e.Handled = true;
     }
     else
         Scanner.CheckForScannedData(sender, ref e);
         e.Handled = false;
 }

 private void txtAantalPress(object sender, View.KeyEventArgs e)
 {
     if (e.KeyCode == Keycode.Escape && e.Event.Action == KeyEventActions.Down)
     {
         if (txtArtikel.RequestFocus())
             e.Handled = true;
     }
     else
         e.Handled = false;
 }  
NiAu
  • 535
  • 1
  • 12
  • 32

1 Answers1

0

You don't need to handle the KeyPress event in order to set the focus to the next EditText. Android has a good algorithm that handles the focus when editing is done.

How does the app behave on pressing enter when you remove the event handler? Does it set the focus to the correct EditText? If it focuses he wrong view then you should take a look on how to Move to another EditText when Soft Keyboard Next is clicked on Android.

If the focus doesn't change at all then you should tell Android what to do when the user has completed his input by using android:imeOptions: http://developer.android.com/guide/topics/ui/controls/text.html#Actions.

Community
  • 1
  • 1
Wosi
  • 41,986
  • 17
  • 75
  • 82
  • thx for your answer. I tried the imeOptions, but with the actionNext it just goes to a new line in the same editText. This also happens when the event handler is removed.On a Keydown I also need to check if the editText contains any text. – NiAu Nov 05 '15 at 09:00