开发环境:VS2017 pro (C#)
在实测xamarin中EditText的keypress事件,发现其每按下1个数字或其他按键时会导致其触发两次(但在winform中未有此现象,可能为bug)……
为了避免此问题,
- 愚将在base.OnCreate中绑定keypress事件的代码取消,
- 将其移到了editText的TextChanged事件中绑定,
- 并在keypress事件中将keypress事件取消,以此避免其会多次触发。
在TextChange中绑定事件
private void TxtQueryID_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
{
EditText txtQueryID = FindViewById<EditText>(Resource.Id.txtQueryID);
txtQueryID.KeyPress += TxtQueryID_KeyPress;
}
在KeyPress中取消事件
private void TxtQueryID_KeyPress(object sender, View.KeyEventArgs e)
{
EditText txtQueryID = FindViewById<EditText>(Resource.Id.txtQueryID);
e.Handled = false;// e.Handled = true则会阻止输入,但xamarin中不设置为false就不能
//输入,也就是说xamarin中可能默认其e.handled为true,或不支持
txtQueryID.KeyPress -= TxtQueryID_KeyPress;//取消绑定
if (e.KeyCode != Keycode.Enter)
{
return;
}else
{
//your code
}
}