如何限制 Android 中的 TextView 仅允许字母数字字符

发布于 2024-11-09 05:56:56 字数 57 浏览 0 评论 0原文

我的应用程序中有一个 TextView,我希望用户只能在其中输入字母数字字符。如何做到这一点?谢谢!

I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!

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

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

发布评论

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

评论(4

与之呼应 2024-11-16 05:56:56

在 XML 中,输入以下内容:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

In the XML, put this:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
屋檐 2024-11-16 05:56:56

这是一个更好的解决方案...... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter});

Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter});
满天都是小星星 2024-11-16 05:56:56

InputFilter 解决方案运行良好,让您可以完全控制以比 android:digits 更细的粒度过滤输入。如果所有字符都有效,则 filter() 方法应返回 null;如果某些字符无效,则应返回仅包含有效字符的 CharSequence。如果复制并粘贴了多个字符,并且其中一些字符无效,则应仅保留有效字符(如果任何字符无效,@AchJ 的解决方案将拒绝整个粘贴)。

public static class AlphaNumericInputFilter implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {

        // Only keep characters that are alphanumeric
        StringBuilder builder = new StringBuilder();
        for (int i = start; i < end; i++) {
            char c = source.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                builder.append(c);
            }
        }

        // If all characters are valid, return null, otherwise only return the filtered characters
        boolean allCharactersValid = (builder.length() == end - start);
        return allCharactersValid ? null : builder.toString();
    }
}

另外,在设置 InputFilter 时,必须确保不要覆盖 EditText 上设置的其他 InputFilters;这些可以在 XML 中设置,例如 android:maxLength。您还必须考虑 InputFilters 设置的顺序。当与长度过滤器结合使用时,您的自定义过滤器应插入到长度过滤器之前,这样粘贴的文本会在长度过滤器之前应用自定义过滤器(@AchJ 的解决方案将覆盖所有其他 InputFilters 并且仅应用自定义的)。

    // Apply the filters to control the input (alphanumeric)
    ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(editText.getFilters()));
    curInputFilters.add(0, new AlphaNumericInputFilter());
    InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
    editText.setFilters(newInputFilters);

The InputFilter solution works well, and gives you full control to filter out input at a finer grain level than android:digits. The filter() method should return null if all characters are valid, or a CharSequence of only the valid characters if some characters are invalid. If multiple characters are copied and pasted in, and some are invalid, only the valid characters should be kept (@AchJ's solution will reject the entire paste if any characters a invalid).

public static class AlphaNumericInputFilter implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {

        // Only keep characters that are alphanumeric
        StringBuilder builder = new StringBuilder();
        for (int i = start; i < end; i++) {
            char c = source.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                builder.append(c);
            }
        }

        // If all characters are valid, return null, otherwise only return the filtered characters
        boolean allCharactersValid = (builder.length() == end - start);
        return allCharactersValid ? null : builder.toString();
    }
}

Also, when setting your InputFilter, you must make sure not to overwrite other InputFilters set on your EditText; these could be set in XML, like android:maxLength. You must also consider the order that the InputFilters are set. When used in conjunction with a length filter, your custom filter should be inserted before the length filter, that way pasted text applies the custom filter before the length filter (@AchJ's solution will overwrite all other InputFilters and only apply the custom one).

    // Apply the filters to control the input (alphanumeric)
    ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(editText.getFilters()));
    curInputFilters.add(0, new AlphaNumericInputFilter());
    InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
    editText.setFilters(newInputFilters);
筱武穆 2024-11-16 05:56:56

这应该有效:

textView.setInputType(InputType.TYPE_CLASS_NUMBER);

This should work:

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