有人能帮助我与参数到Android输入过滤"滤波器QUOT;方法? (加正则表达式) [英] Can someone help me with the parameters to the Android InputFilter "filter" method? (plus regex)

查看:134
本文介绍了有人能帮助我与参数到Android输入过滤"滤波器QUOT;方法? (加正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人能向我解释的源和目标参数的 <目的code> android.text.InputFilter#过滤器

Please someone can explain to me the purpose of the source and dest parameters in android.text.InputFilter#filter?

我想阅读的文档,但我真的很困惑。我试图用一个正则表达式来使一个IP掩码。任何帮助是AP preciated。

I tried to read the docs but I am really confused. I am trying to use a regex to make an IP mask. Any help is appreciated.

我现在明白了。因此,举例来说,如果我有123.42,则该用户类型123.42d,我将有:

I get it now. So, for example, if I have 123.42, then the user types 123.42d, I will have:

dest = 123.42  
source = 123.42d  
start = 5  
end = 6

InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter() 
    {
        public CharSequence filter(CharSequence source, int start, int end, Spanned  dest, int dstart, int dend) 
        {               
            String destTxt = dest.toString();
            String resultingTxt = destTxt.substring(0, dstart) +                           source.subSequence(start, end) + destTxt.substring(dend);                

            if(resultingTxt.equals("")) return "";

            int lastChar = resultingTxt.length() -1;

            if(String.valueOf(resultingTxt.charAt(lastChar)).matches("[^0-9.]"))
            {
                return "";
            }

            return null;
        }
    };

这不,虽然工作。如果不是这回我只是数字?碰巧这取决于用户键入它返回我字写得

This isn't working though. Shouldn't this return me only the digits? It happens that depending on what the user type it returns me characters too.

推荐答案

如果你有一个的EditText 键,您分配一个输入过滤来,然后每次你改变在那里过滤器()方法将被调用。就像一个按钮的onClick()方法。

If you have an EditText and you assign an InputFilter to it, then everytime you change the text in there the filter() method will be called. Much like the onClick() method of a button.

比方说,你在编辑它之前,必须在你的EditText文本Hello Androi。如果preSS
您的虚拟键盘上的 D 键,然后输入过滤器被触发,基本上问,如果它是确定添加 D

Let's say you had the text "Hello Androi" in your EditText before editing it. If you press the D key on your virtual keyboard then the inputfilter is triggered and basically asked if it is ok to add a d.

在这种情况下,是Android的,开始是6,到底是7 - 那是你提到新的文本

In that case source is "Android", start is 6, end is 7 - That is your reference to the new Text.

DEST 将是Androi,指的是旧的文本在的EditText

dest would be "Androi" and refers to the old text in your EditText

所以,你得到新的String,并在该字符串,你必须检查它是否是一个好位置(6,7)。如果你只想得到一个单一的字符(如 D ),你不能决定,如果如你刚才输入的号码形成了一个IP地址。您需要整个文本在某些情况下的上下文。

So you get the new String and a position in that string (the 6,7) that you have to check if it is okay. If you would just get a single character (like the d) you could not decide if e.g. the number you just entered forms an ip adress. You need the whole text as a context in some cases.

如果新的文本是确定的是回报率,如果你想跳过一个改变返回空的字符串(),否则返回更换变化的字符。

If the new text is ok as is return null, if you want to skip a change return empty String (""), otherwise return the characters that replace the change.

因此​​,一个简单的例子可能是:

So a simple example might be that:

/**
 * Simplified filter that should make everything uppercase
 * it's a demo and will probably not work
 *  - based on InputFilter.AllCaps
 */
public static class AllCaps implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {

        // create a buffer to store the edited character(s).
        char[] v = new char[end - start];

        // extract the characters between start and end into our buffer
        TextUtils.getChars(source, start, end, v, 0);

        // make the characters uppercase
        String s = new String(v).toUpperCase();

        // and return them
        return s;
    }
}

它替换为它的大写版本的每一个变化。

It's replacing every change with the uppercase version of it.

这篇关于有人能帮助我与参数到Android输入过滤&QUOT;滤波器QUOT;方法? (加正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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