NumberPicker 不适用于 Xamarin 中的键盘输入值 Android

NumberPicker does not work with keyboard input value in Xamarin Android

我在 android activity 中有一个数字选择器控件。单击那些“+”和“-”按钮时效果很好。但是当我从键盘输入一个数字并尝试在程序中获取当前输入的值时,它不会给出键盘输入的值。我正在使用 C# Xamarin android.

是否有任何按键事件或可以提供帮助的东西?

您可以在 NumberPicker 的 EditText 上创建并设置自定义 OnEditorActionListener:

NumberPicker picker;
protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.test);
            picker  = FindViewById<NumberPicker>(Resource.Id.numberPicker);
            picker.MaxValue = 40;
            picker.MinValue = 1;
            EditText editText= (EditText)picker.GetChildAt(1);
            editText.SetOnEditorActionListener(new CustomActionListener());

        }

 public class CustomActionListener : Java.Lang.Object,EditText.IOnEditorActionListener
        {

            public bool OnEditorAction(TextView v, Android.Views.InputMethods.ImeAction actionId, KeyEvent e)
            {

                if(actionId == Android.Views.InputMethods.ImeAction.Done)
                {
                    // Here is you number
                    string countNumber = v.Text;
                }
                return true;

            }

            public IntPtr Handle
            {
                get { return base.Handle; }
            }

            public void Dispose()
            {
                base.Dispose();
            }
        }