Xamarin Android Edittext Keypress也执行

时间:2015-11-04 15:01:55

标签: c# android xamarin android-edittext

您好我刚开始使用Android项目的Xamarin,我遇到了一个关于edittext按键的问题。在我的第一个Edittext上触发Enter并且它将焦点放在Edittext(txtArtikel)上,然后触发此Edittext的按键并跳转到第三个Edittext。所以它只用一个Enter按下就跳过我的第二个Edittext。任何人都可以帮助我吗?

 txtArtikel.KeyPress += txtArtikelPress;

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

修改

我仍在使用Keypress,现在正在寻找Enter和Escape。当txtWerf为空时它跳转到txtArtikel,但是当它包含文本时它会转到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;
 }  

1 个答案:

答案 0 :(得分:0)

您不需要处理KeyPress事件,以便将焦点设置为下一个EditText。 Android有一个很好的算法,可以在编辑完成后处理焦点。

删除事件处理程序时,按Enter键时应用程序的行为如何?它是否将焦点设置为正确的EditText?如果它关注他错误的观点那么你应该看看如何 Move to another EditText when Soft Keyboard Next is clicked on Android

如果焦点根本没有变化,那么当用户使用android:imeOptionshttp://developer.android.com/guide/topics/ui/controls/text.html#Actions完成输入时,您应该告诉Android该怎么做。