c# - 检查 EditText 的字符数

标签 c# android xamarin xamarin.android

我通过 Xamarin (C#) 编写 Android 应用

我有 EditText 字段。并检查 NullOrEmpty。

            if (string.IsNullOrEmpty (ulitsa.Text) ) {
                Toast.MakeText (this, "Заполните поле 'Ваша Улица'", ToastLength.Long).Show ();
            }

我想设置最大和最小字符过滤器。 Min-3,Max-6,如果用户没有这个字符数显示 toast 通知。

我如何实现这一点?

最佳答案

myEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                //Here, get text length:
                Integer len = myEditText.getText().toString().length();

               // Then, depending on the length display the toast and do what you want
            }
        });

但实际上您可以通过编程方式设置最大长度:

InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(6);
myEditText.setFilters(filterArray);

关于c# - 检查 EditText 的字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35033760/

相关文章:

c# - Xamarin 表单按钮 OnClick

c# - 泛型参数的可空返回值

c# - 如何将适度大量的数据传递到我的 Web API 应用程序?

java - 标签缺少错误 eclipse

c# - MonoTouch 支持 C# 动态吗?

xamarin - 如何将 iOS 中 Selector Rendere 的颜色更改为新颜色?

c# - 阻止用户选择但允许以编程方式选择

c# - 在每行算法中找到最高分

android - 移动设备上的可滚动 OL3 LayerSwitcher

android - Realm 对象覆盖 equals 方法