简体   繁体   English

Android限制用户输入

[英]Android restrict user input

I'm trying to restrict user input. 我试图限制用户输入。 If the user inputs consecutive digits eg,like this 1232334, and consecutive special characters eg, !@%$^&#@$*. 如果用户输入连续数字(例如1232334)和连续特殊字符(例如!@%$ ^&#@ $ *)。 But it will allow user to input ordinal numbers like 1st, 2nd, etc. And also it will allow John 13:12. 但是它将允许用户输入第一个,第二个等序数。并且还将允许约翰福音13:12。 A verse type. 诗歌类型。

This is what I've tried: (I used InputFilter) 这是我尝试过的:(我使用了InputFilter)

 InputFilter filter = new InputFilter() { 
        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {
            // TODO Auto-generated method stub
            for (int i = start; i < end; i++) { 
                if (!Character.isLetterOrDigit(source.charAt(i))) { 
                    return ""; 
                    } 
             } 
             return null; 
        } 
    }; 

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

But it won't let me type special characters, and it can accept consecutive digits. 但是它不允许我键入特殊字符,并且可以接受连续的数字。 Does anyone know how to achieve my requirements? 有谁知道如何达到我的要求? Your is pretty much appreciated. 非常感谢您。 Thanks. 谢谢。

Try this Tested solution. 试试这个经过测试的解决方案。

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText et;
    String specialCharSet = "!@%$^&#@$*";

    InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String tmp;
            try {
                tmp = (et.getText().toString()).substring(et.getText().toString().length() - 2, et.getText().toString().length());

                if (Character.isDigit(tmp.charAt(0)) && Character.isDigit(tmp.charAt(1)) && Character.isDigit(source.toString().charAt(0))) {
                    return "";
                }

                if (specialCharSet.contains("" + tmp.charAt(1)) && specialCharSet.contains("" + source)) {
                    return "";
                }
            } catch (Exception e) {
                if (et.getText().toString().length() >= 1 && specialCharSet.contains("" + et.getText().toString().charAt(0)) && specialCharSet.contains(source)) {
                    return "";
                }
            }

            return null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        et.setFilters(new InputFilter[] { filter });

    }

}

I suggest to change your implementation the following way: 我建议通过以下方式更改您的实现:

  • Build Regular Expressions for both patterns of input you allow 为您允许的两种输入模式构建正则表达式
  • Build the string that would result from the user's input with the information from source, start, end, dest, dtsart and dend 使用来自源,开始,结束,dest,dtsart和dend的信息构建将由用户输入产生的字符串
  • Check if this string matches one of your two regular expressions, if not return "" 检查此字符串是否与您的两个正则表达式之一匹配,如果不匹配则返回“”

That should do it more or less. 那应该或多或少地做到这一点。 I've done something similar a few month ago. 我几个月前做了类似的事情。 If you're lucky, I can find the code and paste it here, when I'm home this evening :) But in general this should be pretty straight forward. 如果幸运的话,今天晚上我回家时,我可以找到代码并将其粘贴在这里:)但总的来说,这应该很简单。 If you're unsure about the values of source, start, end, dest, dstart and dend, check them in the debugger, to get a better understanding. 如果不确定source,start,end,dest,dstart和dend的值,请在调试器中检查它们,以更好地理解。

-- Update -- The good thing is, I found my code in a question I once posted here ;) -更新-好处是,我在一次张贴在这里的问题中找到了代码;)

public class MoneyFilter implements InputFilter {

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

        // The regex to test the string
        String testRegex = "^\\d+\\,?\\d{0,2}$";

        String s = source.toString();
        String d = dest.toString();

        // Build the string that would return from the user's input
        String r = d.substring(0, dstart) + s.substring(start, end)
            + d.substring(dend);

        // And check it
        if (r.matches(testRegex)) {
            return null;
        } else {
            return "";
        }
    }
}

You'll off course need different regular expressions to match the string, but the rest of the code should work. 当然,您将需要不同的正则表达式来匹配字符串,但是其余的代码应该可以工作。

hope this helps, 希望这可以帮助,

regards, 问候,

Hans 汉斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM