2013-04-18 119 views
10

几周前我开始与Xamarin Studio合作,并且找不到解决下一个问题的方法: 创建了一个包含序列号的edittext。在按下输入后,我想让ro运行一个功能。 它工作正常,当我按输入,函数运行没有失败,但我不能修改edittext的内容(我不能键入它)。Xamarin Android EditText输入密钥

代码:

EditText edittext_vonalkod = FindViewById<EditText>(Resource.Id.editText_vonalkod); 
edittext_vonalkod.KeyPress += (object sender, View.KeyEventArgs e) => 
{ 
    if ((e.Event.Action == KeyEventActions.Down) && (e.KeyCode == Keycode.Enter)) 
    { 
     //Here is the function 
    } 
}; 

这是控制的代码:

<EditText 
    p1:layout_width="wrap_content" 
    p1:layout_height="wrap_content" 
    p1:layout_below="@+id/editText_dolgozo_neve" 
    p1:id="@+id/editText_vonalkod" 
    p1:layout_alignLeft="@+id/editText_dolgozo_neve" 
    p1:hint="Vonalkód" 
    p1:text="1032080293" 
    p1:layout_toLeftOf="@+id/editText_allapot" /> 

我试图用edittext_vonalkod.TextCanged与它的参数,这个问题保留。我可以修改内容但不能处理输入键。

谢谢!

回答

5

当按下ENTER键时,您需要将事件标记为未处理。将下面的代码放入KeyPress处理程序中。

if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) 
{ 
    // Code executed when the enter key is pressed down 
} 
else 
{ 
    e.Handled = false; 
} 
9

最好的办法是使用被设计为触发对输入按键的EditorAction事件。这将是这样的代码:

edittext_vonalkod.EditorAction += (sender, e) => { 
    if (e.ActionId == ImeAction.Done) 
    { 
     btnLogin.PerformClick(); 
    } 
    else 
    { 
     e.Handled = false; 
    } 
}; 

,并能够改变的文本输入按钮使用imeOptions你的XML:

<EditText 
    p1:layout_width="wrap_content" 
    p1:layout_height="wrap_content" 
    p1:layout_below="@+id/editText_dolgozo_neve" 
    p1:id="@+id/editText_vonalkod" 
    p1:layout_alignLeft="@+id/editText_dolgozo_neve" 
    p1:hint="Vonalkód" 
    p1:text="1032080293" 
    p1:layout_toLeftOf="@+id/editText_allapot" 
    p1:imeOptions="actionSend" /> 
3

试试这个:



    editText = FindViewById(Resource.Id.editText);  
    editText.KeyPress += (object sender, View.KeyEventArgs e) => 
    { 
     e.Handled = false; 
     if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) 
     { 
      //your logic here 
      e.Handled = true; 
     } 
    }; 

0

更好的是创建EditText(EditTextExtensions.cs)的可重用扩展:

public static class EditTextExtensions 
{ 
    public static void SetKeyboardDoneActionToButton(this EditText editText, Button button) 
    { 
     editText.EditorAction += (sender, e) => { 
      if (e.ActionId == ImeAction.Done) 
      { 
       button.PerformClick(); 
      } 
      else 
      { 
       e.Handled = false; 
      } 
     }; 
    } 
}