如何使用正EX pression在安卓如何使用、EX、pression

2023-09-13 01:32:45 作者:乜づ狠傷ぺ

我有一个numberDecimal 的EditText ,我想用常规的前pression验证。在验证我想是。

小数点之前,我想进入的最大数字是三个数字不应以零喜欢2,23,342等

在小数点我想进入的最大数字是一个像。1,3,0.6等等

所以,我允许用户输入的数字就像是2.1,32.5,444.8,564.9等

但是,在我的code发生什么事是 -

它允许用户小数点像3456,4444,5555前输入更多的则三位数之后,它不会让我以后进入小数点。

这让我小数点数字开始前输入0。

那么,为什么出现这种情况,是什么错误的,我经常EX pression我都习惯了。如果有谁知道帮我解决了这一点。

code我用的是

  weightEditText.addTextChangedListener(新TextWatcher()
    {
        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
        }
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){
        }
        @覆盖
        公共无效afterTextChanged(编辑S)
        {
            图案mPattern = Pattern.compile(^([1-9] [0-9] {0,2})(\\ [0-9])$???);

            匹配器匹配= mPattern.matcher(s.toString());
            如果(!matcher.find())
            {
                weightEditText.setText(); //不知道该怎么地点
            }
        }
    });
 

解决方案

从未有在审查的任何一点 DEST 独自一人在一个输入过滤;这是什么已经$的指示区P $ psent。改变常规的前pression比赛是反对,这将是适当的,如果你只是想检查某些字符被接受进入该领域。但是,要检查字段格式,而不只是过滤输入一个字符逐个字符的基础。这是更复杂的

每当用户更改为 tempEditText 的内容时,系统调用过滤器的过滤器方法在的变化实际上是由。它通过当前字段的内容和提议的改变(可插入/添加,删除或替换)。这种变化是重新由源psented $ P $ 的CharSequence源(字符—如有—被添加到现场),范围源中开始和结束的索引(该范围不一定所有的),一个跨区DEST (改变之前,在当前字段内容)和范围提出通过指定源取代了在 DEST DSTART和DEND指数系列。

过滤器是修改的变化(如果需要),并返回作业的的CharSequence 来使用(在其整体)在(或来继续使用)。而不是检查 DEST 因为你现在做什么,你将需要检查的变化是否会导致一个可接受的领域。要做到这一点,则需要更复杂的逻辑。 (请注意,特别是新的字符(县)可用于插入较底以外的地方;还有,过滤器用户删除字符时,将被称为以及将它们添加。)

这可能是更容易实现TextWatcher.在它的 beforeTextChanged 方法,你可以记录当前的内容和它的 afterTextChanged 方法,你可以检查(使用普通前pression)中的内容是否是可接受的,如果不是,恢复​​之前的非变化的内容。 (请确保,虽然,更改前的文字是可以接受的。如果不是这样,替代的东西可接受mdash;如清除该领域,否则你的code将进入一个无限循环,因为 TextWatcher 将被再次调用时,你更正字段内容。)

Expression Blend 4 step10

您还可以在你的常规的前pression的错误:它允许一个前导零。下面是修复此问题(并删除一组不必要的括号)的改良版:

 ^([1-9] [0-9] {0,2})?(\\。[0-9]?)?$
 

(顺便说一句:你可以使用 \\ð而不是 [0-9]

修改

下面是你编辑的我的编辑:

  weightEditText.addTextChangedListener(新TextWatcher()
{
    私有静态最终模式sPattern
        = Pattern.compile(^([1-9] [0-9] {0,2})(\\ [0-9])$???);

    私人CharSequence的多行文字;

    私人布尔参考isValid(CharSequence中){
        返回sPattern.matcher(多个).matches();
    }

    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    }

    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            之后INT){
        多行文字=的isValid(S)?新的CharSequence(S):;
    }

    @覆盖
    公共无效afterTextChanged(编辑S)
    {
        如果(!的isValid(多个))
        {
            weightEditText.setText(MTEXT);
        }
        多行文字= NULL;
    }
});
 

I have an numberDecimal EditText which i want to validate using regular expression. In validation what i want is.

Before Decimal point the maximum digit i want to enter is three and the digit should not start with zero like 2,23,342 etc

After Decimal point the maximum digit i want to enter is one like .1, .3, .6 etc

So the number that i allow user to enter is like 2.1, 32.5, 444.8, 564.9 etc

But in my code what happen is-

It allow user to enter more then three digit number before decimal point like 3456, 4444, 5555 and after that it doesn't allow me to enter decimal point after that.

It allow me to enter 0 before decimal point as the start of the digit.

So why this happen, is anything wrong in my regular expression i have used. If anyone knows help me to solve this out.

Code I have used is

weightEditText.addTextChangedListener(new TextWatcher() 
    {           
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){                
        }           
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after){         
        }           
        @Override
        public void afterTextChanged(Editable s) 
        {
            Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");

            Matcher matcher = mPattern.matcher(s.toString());               
            if(!matcher.find())
            {
                weightEditText.setText(); // dont know what to place                    
            }
        }
    });

解决方案

There's never any point in examining dest alone in an InputFilter; that's what's already present in the field. Change the regular expression match to be against source and it would be appropriate if you only wanted to check that certain characters were accepted into the field. However, you want to check field formatting, not just filter the input on a character-by-character basis. This is much more complex.

Every time the user makes a change to the contents of tempEditText, the system calls your filter's filter method before the change is actually made. It passes the current field contents and the proposed change (which can be insert/append, delete, or replace). The change is represented by a source CharSequence source (the characters—if any—to be added to the field), range start and end indexes within the source (the range is not necessarily all of source), a Spanned dest (the current field contents before the change) and range dstart and dend indexes within dest that are proposed to be replaced by the indicated source range.

The job of filter is to modify the change (if necessary) and return a CharSequence to use (in its entirety) in place of source (or null to go ahead and use source). Rather than checking dest as you are now doing, you will need to check whether the change will result in an acceptable field. To do this, you will need more complex logic. (Note, in particular, that the new character(s) may be intended for insert somewhere other than at the end; also, filter will be called when the user is deleting characters as well as adding them.)

It may be easier to implement a TextWatcher. In it's beforeTextChanged method, you can record the current contents and in it's afterTextChanged method, you can check (using a regular expression) whether the contents are acceptable and, if not, restore the before-the-change contents. (Make sure, though, that the text before the change was acceptable. If it isn't, substitute something acceptable—like clearing the field. Otherwise your code will go into an infinite loop because the TextWatcher is going to be invoked again when you correct the field contents.)

You also have an error in your regular expression: it allows a leading zero. Here's an improved version that fixes this problem (and removes one set of unnecessary parentheses):

"^([1-9][0-9]{0,2})?(\\.[0-9]?)?$"

(As an aside: you can use \\d instead of [0-9].)

EDIT

Here's my edit of your edit:

weightEditText.addTextChangedListener(new TextWatcher() 
{           
    private static final Pattern sPattern
        = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");

    private CharSequence mText;

    private boolean isValid(CharSequence s) {
        return sPattern.matcher(s).matches();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count){
    }           

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after){
        mText = isValid(s) ? new CharSequence(s) : "";
    }           

    @Override
    public void afterTextChanged(Editable s) 
    {
        if (!isValid(s))
        {
            weightEditText.setText(mText);
        }
        mText = null;
    }
});