EditText 有内置的过滤选项吗?

发布于 2024-11-08 12:16:36 字数 249 浏览 0 评论 0原文

是否可以将过滤器(我不确定它的名称)附加到 EditTexts,以便如果用户以错误的方式键入某些内容,则 EditText 上会弹出某种警告?

例如:表单上有一个 EditText,必须填写它。因此,在它为空之前,其中会显示一些警告,表明它是表单上的必填字段。

我在 Android 上的 EditTexts 上看到过类似的行为,所以我猜有一个内置功能,但我找不到它。有人可以帮我吗?

目标 Android 版本为 3.0 及更高版本。

Is it possible to attach filters (I'm not sure about the name of this) to EditTexts, so that if the user types in something in the wrong manner, than a warning of some sort pops up on the EditText?

For example: there's an EditText on a form and it must be filled. So until it's empty, some warning is displayed in it, showing that it's a required field on the form.

I've seen similar behaviour on EditTexts all around Android, so I guess there's a built-in feature for this, but I can't find it. Could somebody help me out please?

Target Android version is 3.0 and up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-11-15 12:16:36

您可能需要添加 TextWatcher 和在 public void afterTextChanged(Editable s) 的实现中做出相应的反应。只需检查 s 是否为空,或者是否正确。

您也可以实现过滤器,但是那些通常是为了防止用户输入错误的数据。

You may want to add a TextWatcher and react accordingly in your implementation of public void afterTextChanged(Editable s). Just check if s is empty or not, or if it's correct or not.

You can implement filters also, but those are usually to prevent the user to entering wrong data.

剪不断理还乱 2024-11-15 12:16:36

查看xml中EditTextandroid:inputType属性。例如 android:inputType="textEmailAddress|number|phone" 和其他内容。
但是为了检查空性,除了 if (editText.getText().length() > 0) 之外,我无法建议任何其他内容。

Look at android:inputType attribute of EditText in xml. For example android:inputType="textEmailAddress|number|phone" and other stuff.
But for checking emptiness I can't suggest anything but if (editText.getText().length() > 0).

听不够的曲调 2024-11-15 12:16:36

您可以使用带有文本观察器事件的代码添加过滤器。当您在编辑文本上输入任何想法时,文本观察器事件将被执行,然后我们可以检查编辑文本中的类型文本,如果条件为假,那么您应该显示弹出窗口。我展示了一个在文本观察器事件的帮助下使用编辑文本在列表视图中进行过滤的示例,您应该在编辑文本中进行过滤。

ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            fillMaps.clear();
            textlength = ed.getText().length();
            for (int i = 0; i < countryName.length; i++) {
                if (textlength <= countryName[i].length()) {
                    if (ed.getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) countryName[i].subSequence(0,
                                            textlength))) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("flag", "" + imageId[i]);
                        map.put("country", countryName[i].toString());
                        map.put("capital", capitalName[i].toString());
                        map.put("countrytime",
                                convertDateTimeToGMT(
                                        GMTplusMinusInMillisecond[i],
                                        plusMinus[i]));
                        map.put("GMT", GMTplusMinus[i].toString());

                        fillMaps.add(map);
                    }
                }
            }
            SimpleAdapter adapter = new SimpleAdapter(
                    WorldClockActivity.this, fillMaps, R.layout.grid_item,
                    from, to);
            lv1.setAdapter(adapter);
            // lv1.setAdapter(new
            // ArrayAdapter<String>(WorldClockActivity.this,android.R.layout.simple_list_item_1
            // , arr_sort));

        }
    });

You can add filter using code with Text watcher event. When you type any think on Edit Text the text watcher event is execute then we are able to check the type text in edit text if condition is false then you should show popup . I am showing a example for filtering in list view using edit text with the help of text watcher event you should you it for filtering in edittext.

ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            fillMaps.clear();
            textlength = ed.getText().length();
            for (int i = 0; i < countryName.length; i++) {
                if (textlength <= countryName[i].length()) {
                    if (ed.getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) countryName[i].subSequence(0,
                                            textlength))) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("flag", "" + imageId[i]);
                        map.put("country", countryName[i].toString());
                        map.put("capital", capitalName[i].toString());
                        map.put("countrytime",
                                convertDateTimeToGMT(
                                        GMTplusMinusInMillisecond[i],
                                        plusMinus[i]));
                        map.put("GMT", GMTplusMinus[i].toString());

                        fillMaps.add(map);
                    }
                }
            }
            SimpleAdapter adapter = new SimpleAdapter(
                    WorldClockActivity.this, fillMaps, R.layout.grid_item,
                    from, to);
            lv1.setAdapter(adapter);
            // lv1.setAdapter(new
            // ArrayAdapter<String>(WorldClockActivity.this,android.R.layout.simple_list_item_1
            // , arr_sort));

        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文