2017-10-17 14 views
0

私のxamarin.androidプロジェクトでは、edittextが使用されているscreenがあります。これはnumberdecimal型です。ここでは、keyPressでtextchangedイベントを取得していて、以下のようなメソッドを使用して左に という数値を追加しています。 ここにコードがあります。UITextField KeyPress Handler in Xamarin.IOS

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..andで全部を複製したいです。誰でもこの1つを達成するために私を ガイドしてもらえますか?私に知らせてください。これを行うための他の回避策がありますか?ここで

は、それがiOS版で異なる包まれていながらもあなたが非常に簡単に、それを達成することができます

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(); 
     } 
} 

答えて

0

(不完全)xamarin.iosコードです。代表者とそれを扱う、ないのEventHandler

はあなたのコントローラで、thisにあなたのTextFieldのデリゲートを設定します。

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

field.Delegate = this; 

すると、お使いのコントローラがIUITextFieldDelegate

は、その後、次のメソッドを実装して実装していることを確認します

これは、テキストが変更されるたびに呼び出され、関数から値を取得することもできます。