在EditText上实现ITextWatcher会抛出错误
问题描述:
好的,所以我设置了一个全包控件来接受扫描输入,然后在文本发生变化时执行一些功能。我在我的EditText上实现了ITextWatcher,当我导航到带有该控件的片段时,出现错误。下面是在控制的必要因素在EditText上实现ITextWatcher会抛出错误
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();
}
}
然后我浏览到任何片段这种控制,并得到了以下错误: System.NotSupportedException:无法启动类型AndroidMobileBarcodeForMieTrakAPI.Xamarin.Controls.ScannerEditText实例原生手柄
现在,如果我删除ITextWatcher接口和实现其他一切工作
一切我搜索后看到的大多是java的其中工程有点不同OVE而不是实施这些成员。
答
您需要与IntPtr
和JniHandleOwnership
暴露的构造:
protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
这是真棒,谢谢你。 –