输入所需格式的文本后,如何停止编辑文本以添加文本? [英] How to stop the edit text to add text,when the required format text is entered in it?

查看:50
本文介绍了输入所需格式的文本后,如何停止编辑文本以添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,当用户输入10位数字,一个点和两个小数点(0123456789.00).一旦用户输入此格式,编辑文本应自动停止向其中添加文本.用户输入的字符不得超过一个点.

What is my requirement is when the user enter 10 digits,a Dot and two decimal points(0123456789.00).Once the user entered this format automatically the edit text should stop adding text into it.And the user should not enter more than one dot.

是否可能..?.需要帮助

is it possible..?.need help

先谢谢..!

推荐答案

您可以使用 editText.setFilters(filter)将文本过滤器应用于 EditText ,因此使用的方法将如下所示:

You can apply text filter to your EditText by using editText.setFilters(filter), So the method used will be like the following:

text = (EditText) findViewById(R.id.text);
validate_text(text);

// the method validate_text that forces the user to a specific pattern
protected void validate_text(EditText text) {

    InputFilter[] filter = new InputFilter[1];
    filter[0] = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {

            if (end > start) {
                String destText = dest.toString();
                String resultingText = destText.substring(0, dstart)
                        + source.subSequence(start, end)
                        + destText.substring(dend);
                if (!resultingText
                        .matches("^\\d{1,10}(\\.(\\d{1,2})?)?")) {
                    return "";
                } 
            }

            return null;
        }
    };
    text.setFilters(filter);
}

这将迫使用户在输入数字后输入点",并且将迫使他仅在点"之后输入两位".

This will force the user to enter "dot" after entering for digits, and will force him to enter only "two digits" after the "dot".

这篇关于输入所需格式的文本后,如何停止编辑文本以添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆