我已将EditText控件的Keypress
和EditorAction
事件处理程序合并到一个名为 InputField_KeyPressEditorAction 的方法中。当我输入一些文本并按Enter键时,以下代码执行多次(2,3或更多):
private void InputField_KeyPressEditorAction(object sender, EventArgs e)
{
var ke = e as View.KeyEventArgs;
var eae = e as TextView.EditorActionEventArgs;
if (ke != null)
{
ke.Handled = false;
}
if (eae != null)
{
eae.Handled = false;
}
if (ke != null && (ke.Event.Action == KeyEventActions.Down || ke.Event.Action == KeyEventActions.Up) &&
(ke.KeyCode == Keycode.Enter || ke.KeyCode == Keycode.Unknown) ||
eae != null && eae.ActionId == ImeAction.Done)
{
if (string.IsNullOrEmpty(_inputField.Text)) return;
_inputField.Text = Regex.Replace(_inputField.Text, @"\t|\n|\r", string.Empty);
if (_notificationArea.Text.Contains(_scanEan))
{
// Following code executes twice....
}
if (ke != null)
{
ke.Handled = true;
}
if (eae != null)
{
eae.Handled = true;
}
var imm = (InputMethodManager)GetSystemService(InputMethodService);
imm.HideSoftInputFromWindow(CurrentFocus.WindowToken, HideSoftInputFlags.None);
}
}
我将事件处理程序分配给控件的部分:
_inputField = FindViewById<EditText>(Resource.Id.editTextScan);
_inputField.KeyPress += InputField_KeyPressEditorAction;
_inputField.EditorAction += InputField_KeyPressEditorAction;
这种奇怪行为的原因是什么?我做错了吗?
答案 0 :(得分:1)
将为每个键up,down,longpress和multiplekey以及10个不同的基于EditText.EditorAction
的操作调用TextView.IOnEditorActionListener
(ImeAction
)。
您应该将keypress和editaction事件处理程序分离为单独的函数。