【发布时间】:2022-01-10 10:22:18
【问题描述】:
我正在尝试实现一个接口,当用户在其上键入单个字符时,该接口将在我的EditText 上进行侦听,这样当发生这种情况时,我将收到一条 Toast 消息,You tried to type a new character 并且当用户清除时,此接口将检查EditText 中的字符长度,并显示一个 Toast 消息,如果 count 为 0,则清除所有文本。这就是 Android.Text.IWatcher 接口的用武之地。我已经在我的主类中实现了它,如下所示...
public class MainActivity : AppCompatActivity,ITextWatcher
{
//after I implemented the interface then these methods were generated by the compiler
public void OnTextChanged(Java.Lang.ICharSequence s, int start, int before, int count)
{
//check the length of characters typed and show a Toast
if (s.Length() > 0)
{
Toast.MakeText(this, "You are typing more text", ToastLength.Long).Show();
}
//if user clears all text also show a Toast
if (s.Length() == 0)
{
Toast.MakeText(this, "You have cleared all the text", ToastLength.Long).Show();
}
}
}
我将接口分配给我的EditText,如下所示。
EditText user_message = chat_view.FindViewById<EditText>(Resource.Id.message_box);
//assign a text watcher to the EditText
user_message.AddTextChangedListener(this);
问题是当我在我的 Android 设备上调试这个应用程序时输入我的 EditText 时没有得到 Toasts,请帮我解决这个问题,谢谢。
【问题讨论】:
标签: c# android xamarin textwatcher