猿问

KeyPress / EditorAction事件处理程序被多次调用

我已经将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;

这种奇怪行为的原因是什么?难道我做错了什么?


精慕HU
浏览 212回答 1
1回答
随时随地看视频慕课网APP
我要回答