0

Très bien, donc je suis en train de configurer un contrôle tout compris pour accepter l'entrée de numérisation puis de faire quelques fonctions comme le texte est changé. J'ai implémenté ITextWatcher sur mon EditText et lorsque je navigue vers un fragment avec ce contrôle, j'obtiens une erreur. Voici ce les éléments nécessaires dans le contrôleImplémentation de ITextWatcher sur EditText renvoie l'erreur

public class ScannerEditText : EditText, View.IOnKeyListener, View.IOnFocusChangeListener, ITextWatcher 
{ 
    private void Init() 
    { 
     this.Focusable = true; 
     this.FocusableInTouchMode = true; 
     //this.SetOnKeyListener(this); 
     this.AddTextChangedListener(this); 
     //this.OnFocusChangeListener = this; 
    } 

    private Timer _refreshTimer = null; 
    private string _priorText = null; 

    public ScannerEditText(Context context) 
     : base(context) 
    { 
     this.Init(); 
    } 

    public ScannerEditText(Context context, IAttributeSet attrs) 
     :base(context, attrs) 
    { 
     this.Init(); 
    } 

    public ScannerEditText(Context context, IAttributeSet attrs, int defStyleAttr) 
     :base(context, attrs, defStyleAttr) 
    { 
     this.Init(); 
    } 

    public ScannerEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) 
     :base(context, attrs, defStyleAttr, defStyleRes) 
    { 
     this.Init(); 
    } 

    void ITextWatcher.AfterTextChanged(IEditable s) 
    { 
     if (this._refreshTimer != null) this._refreshTimer.Dispose(); 
     this._refreshTimer = new Timer(delegate (object state) 
     { 
      this._refreshTimer = null; 
      OnUpdateData(this, new EventArgs()); 
     }, false, 500, 0); 
    } 

    void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after) 
    { 
     this._priorText = s.ToString(); 
    } 

    void ITextWatcher.OnTextChanged(ICharSequence s, int start, int before, int count) 
    { 
     string newText = s.ToString(); 

     //if isEmpty do not advance 
     if (string.IsNullOrWhiteSpace(newText)) 
      return; 

     if (newText.Length < this._priorText.Length) 
     { 
      //if 1 character deleted do not advance 
      if (newText == this._priorText.Substring(0, this._priorText.Length - 1)) 
       return; 
     } 
     else if (this._priorText.Length < newText.Length) 
     { 
      //if 1 character added do not advance 
      if (newText.Substring(0, newText.Length - 1) == this._priorText) 
       return; 
     } 

     UIUtil.HideKeyboard((Activity)this.Context, (View)this); 

     ((View)this.Parent).FindViewById<EditText>(this.NextFocusRightId).RequestFocus(); 
    } 
} 

Je navigue au tout fragment de ce contrôle sur et obtenir l'erreur suivante: System.NotSupportedException: Impossible d'activer instance de type AndroidMobileBarcodeForMieTrakAPI.Xamarin.Controls.ScannerEditText de poignée native

maintenant, si je supprime l'interface ITextWatcher et tout le reste fonctionne la mise en œuvre

tout ce que je vois après la recherche est surtout java qui fonctionne un peu différent Ove guider les membres au lieu de les mettre en œuvre.

Répondre

1

Vous devez exposer le constructeur avec IntPtr et JniHandleOwnership:

protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer) 
    : base(javaReference, transfer) 
{ 
} 
+0

C'est génial, merci. –