Xamarin.IOS 中的 UITextField KeyPress 处理程序 [英] UITextField KeyPress Handler in Xamarin.IOS

查看:21
本文介绍了Xamarin.IOS 中的 UITextField KeyPress 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 xamarin.android 项目中,我有一个使用了 edittext 的屏幕,其类型为 numberdecimal.在这里,我在 keyPress 上获取 textchanged 事件并附加使用下面的方法向左编号.这是代码.

In my xamarin.android project i am having a screen in which edittext have been used, which is of type numberdecimal. here i am getting the textchanged event on keyPress and appending the number to the left using the method like below. Here is the code.

namespace MobileApplication.Droid
{
    [Activity(Label = "TransferScreen2Activity", ScreenOrientation = ScreenOrientation.Portrait, Theme = "@style/MyTheme")]
    public class TransferScreenActivity : BaseActivity, IOnTouchListener
    {
    Button _nextButton;      
    EditText _amountEditText;
    bool _triggerTextChangedforAmountEntry = true;
    protected override async void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.transferscreen2);
        _nextButton = FindViewById<Button>(Resource.Id.btnNext);

            _amountEditText = FindViewById<EditText>(Resource.Id.etTransactionAmount);
            _amountEditText.SetSelection(_amountEditText.Text.Length);

            _amountEditText.TextChanged += _amountEditText_TextChanged;

            _amountEditText.SetOnTouchListener(this);
    }

    private void _amountEditText_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
        {
            if (_triggerTextChangedforAmountEntry)
            {
                _triggerTextChangedforAmountEntry = false;
                string pressedKey = "";
                if (e.Text.Count() < (e.Start + 1))
                {
                    pressedKey = "";
                }
                else
                {
                    pressedKey = e.Text.ToString()[e.Start].ToString();
                }


                if (!string.IsNullOrEmpty(pressedKey))
                {
                    CalculateAmountAfterKeyPress(pressedKey, e.Start);

                }
                else
                {
                    CalculateAmountAfterKeyPress("←", 0);
                }

                _amountEditText.SetSelection(_amountEditText.Text.Length);
                _triggerTextChangedforAmountEntry = true;
            }
        }

    private void CalculateAmountAfterKeyPress(string key, int cursorPosition) //appending method.
        {
            string currentText = "";
            if (key == "←")
            {
                currentText = _amountEditText.Text.Trim().Replace("$", "");
            }
            else
            {
                currentText = _amountEditText.Text.Trim().Remove(cursorPosition, 1).Replace("$", "");
            }

            string result = "0";
            bool isProperValue = false;
            switch (key)
            {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    isProperValue = Common.ShiftLeftandAppendChar(currentText, key, out result);

                    break;
                case "←":
                    isProperValue = Common.ShiftRight(currentText, out result);
                    break;
                case "C":
                    isProperValue = true;
                    result = "0.00";
                    break;
                default:
                    isProperValue = true;
                    break;
            }

            _amountEditText.Text = Resources.GetString(Resource.String.CurrencySymbol) + result;
            if (!isProperValue)
            {
                Common.ShakeAnimation(this, _amountEditText);
            }
        }

    public bool OnTouch(View v, MotionEvent e)
        {
            _amountEditText.OnTouchEvent(e);
            _amountEditText.SetSelection(_amountEditText.Text.Length);
            return true;
        }
}

我的问题是..我想在 Xamarin.ios 中复制整个内容..我对这个 xamarin.ios 很陌生.任何人都可以请指导我实现这一目标.请让我知道是否有其他解决方法可以做到这一点.

My question is ..I want to replicate the whole thing in Xamarin.ios..and i am very new to this xamarin.ios. Can anyone please guide me to achieve this one.Please let me know is there any other workaround to do this one.

这里是 xamarin.ios 代码(不完整)

Here is xamarin.ios code(incomplete)

namespace MobileApplication.iOS
{
    public partial class FundTransferScreenController : BaseViewController
        {
        public FundTransferScreenController(IntPtr handle) : base(handle)
            {
             }

            public override async void ViewDidLoad()
            {
                    base.ViewDidLoad();
            amountValue.ValueChanged += HandleTextChanged; 

                }

        }

        private void HandleTextChanged(object sender, EventArgs e)
        {
            //how to do it like android code..?
        }
    public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            base.TouchesBegan(touches, evt);
            amountValue.ResignFirstResponder();
        }
}

推荐答案

您可以很容易地实现这一点,尽管它在 iOS 中的包装方式有所不同.用委托处理它,而不是 EventHandlers

You can quite easily achieve that, albeit it's been wrapped differently in iOS. Handle it with delegates, not EventHandlers

在控制器中将 TextField 的委托设置为 this:

Set your TextField's delegate to this, in your controller:

field.Delegate = this;

确保你的控制器实现了IUITextFieldDelegate

然后实现以下方法:

    [Export("textField:shouldChangeCharactersInRange:replacementString:")]
    public bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
    {
        return true;
    }

每次文本更改时都会调用它,您也可以从函数中获取值.

That's called every time text changes, and you can also get the value from the function.

这篇关于Xamarin.IOS 中的 UITextField KeyPress 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆