在 EditText 上实现 ITextWatcher 会抛出错误
Implementing ITextWatcher on EditText throws error
好的,所以我正在设置一个全包控件来接受扫描输入,然后在文本更改时执行一些功能。我在我的 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,它的工作方式略有不同,覆盖成员而不是实现它们。
您需要使用 IntPtr
和 JniHandleOwnership
:
公开构造函数
protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
好的,所以我正在设置一个全包控件来接受扫描输入,然后在文本更改时执行一些功能。我在我的 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,它的工作方式略有不同,覆盖成员而不是实现它们。
您需要使用 IntPtr
和 JniHandleOwnership
:
protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}