Я объединил обработчики событий Keypress
и EditorAction
элемента управления EditText в один метод с именем 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;
В чем причина этого странного поведения? Я что-то не так делаю?