2017-02-10 65 views
1

當我嘗試使用單獨的類重用貨幣格式的格式時,會生成錯誤。System.InvalidCastException:指定的轉換在editText.AddTextChangedListener上無效

主程序:

using Android.App; 
using Android.OS; 
using Android.Widget; 

namespace TextWatcher 
{ 
    [Activity(Label = "Main2", MainLauncher = true)] 
    public class Main_Activity1 : Activity 
    { 
     private EditText editText; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      SetContentView(Resource.Layout.Main); 

      editText = FindViewById<EditText>(Resource.Id.editText); 
      var watch = new CurrencyTextWatcher(editText); 

      editText.AddTextChangedListener(watch); 


     } 
    } 
} 

類一旦代碼被執行,做格式化(Better way to Format Currency Input editText?

using Android.Widget; 
using Android.Text; 
using Java.Lang; 
using System; 
using Java.Text; 
using Java.Util; 
using System.Text.RegularExpressions; 

namespace TextWatcher 
{ 

    public class CurrencyTextWatcher : ITextWatcher 
    { 
     private EditText editText; 
     private string lastAmount = ""; 
     private int lastCursorPosition = -1; 

     public CurrencyTextWatcher(EditText txt) 
     { 
      this.editText = txt; 
     } 

     public IntPtr Handle 
     { 
      get 
      { 
       throw new NotImplementedException(); 
      } 
     } 

     public void AfterTextChanged(IEditable s) 
     { 

     } 

     public void BeforeTextChanged(ICharSequence amount, int start, int count, int after) 
     { 
      string value = amount.ToString(); 
      if (!value.Equals("")) 
      { 
       string cleanString = clearCurrencyToNumber(value); 
       string formattedAmount = transformtocurrency(cleanString); 
       lastAmount = formattedAmount; 
       lastCursorPosition = editText.SelectionStart; 
      } 
     } 

     public void OnTextChanged(ICharSequence amount, int start, int before, int count) 
     { 
      if (!amount.ToString().Equals(lastAmount)) 
      { 
       string cleanString = clearCurrencyToNumber(amount.ToString()); 
       try 
       { 
        string formattedAmount = transformtocurrency(cleanString); 
        editText.RemoveTextChangedListener(this); 
        editText.Text = formattedAmount; 
        editText.SetSelection(formattedAmount.Length); 
        editText.AddTextChangedListener(this); 

        if (lastCursorPosition != lastAmount.Length && lastCursorPosition != -1) 
        { 
         int lengthDelta = formattedAmount.Length - lastAmount.Length; 
         int newCursorOffset = Java.Lang.Math.Max(0, Java.Lang.Math.Min(formattedAmount.Length, lastCursorPosition + lengthDelta)); 
         editText.SetSelection(newCursorOffset); 
        } 
       } 
       catch (System.Exception e) 
       { 
        //log something 
       } 
      } 
     } 

     public static string clearCurrencyToNumber(string currencyValue) 
     { 
      string result = ""; 

      if (currencyValue == null) 
      { 
       result = ""; 
      } 
      else 
      { 
       result = Regex.Replace(currencyValue, "[^0-9]", ""); 
      } 
      return result; 
     } 

     public static string transformtocurrency(string value) 
     { 
      double parsed = double.Parse(value); 
      string formatted = NumberFormat.GetCurrencyInstance(new Locale("pt", "br")).Format((parsed/100)); 
      formatted = formatted.Replace("[^(0-9)(.,)]", ""); 
      return formatted; 
     } 

     public static bool isCurrencyValue(string currencyValue, bool podeSerZero) 
     { 
      bool result; 

      if (currencyValue == null || currencyValue.Length == 0) 
      { 
       result = false; 
      } 
      else 
      { 
       if (!podeSerZero && currencyValue.Equals("0,00")) 
       { 
        result = false; 
       } 
       else 
       { 
        result = true; 
       } 
      } 
      return result; 
     } 

     public void Dispose() 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

,發生錯誤,請參閱下面的圖像:

System.InvalidCastException: Specified cast is not valid on editText.AddTextChangedListener

幫助!

+0

指定您得到這些異常的行。 –

+0

什麼行號碼失敗?在logcat中單擊由藍色(文本顯示爲紅色)下劃線的錯誤 – Tasos

回答

0

你TextWatcher的實現需要從Java.Lang.Object爲了繼承一個Android調用包裝(ACW)要創建這樣這個對象可以跨越Java和.Net虛擬機之間

編號:Android Callable Wrappers

因爲要創建一個獨立的觀察者,刪除Dispose並從當前實現Handle(如果需要的話,您將需要覆蓋他們爲Java.Lang.Object的那些)

即:

public class CurrencyTextWatcher : Java.Lang.Object, ITextWatcher 
{ 

    public void AfterTextChanged(IEditable s) 
    { 
     //~~~~ 
    } 

    public void BeforeTextChanged(ICharSequence s, int start, int count, int after) 
    { 
     //~~~~ 
    } 

    public void OnTextChanged(ICharSequence s, int start, int before, int count) 
    { 
     //~~~~ 
    } 
} 

現在您可以實例化並將其指定爲文本觀察器偵聽器:

+0

Tks Suchi!有效! –

相關問題