【问题标题】:Allowing space and enter key in android keyboard在安卓键盘中允许空间和输入键
【发布时间】:2016-05-11 03:05:03
【问题描述】:

我想 EditText 中的 InputFilter 只允许 alpha (a-z 或 A-Z) 空格和 Enter 键。但我只能允许 (a-z 和 A-Z)。

如何留出空格和输入键?

这是我的代码:

protected InputFilter filterAlpha = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        Pattern ps = Pattern.compile("^[a-zA-Z]+$");
        if(!source.equals("") && !ps.matcher(source).matches()){
            //Key not allowed
            return "";
        }
        return null;
    }
};


EditText et_name_eng = (EditText) findViewById(R.id.et_name_eng);
et_name_eng.setFilters(new InputFilter[] {filterAlpha});

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以将您的正则表达式更改为“^[a-zA-Z \r\n]+$”。

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2017-05-04
      • 2013-09-28
      • 2014-12-14
      • 2015-07-27
      • 2013-12-06
      • 2015-08-22
      • 2012-10-08
      • 2011-12-07
      相关资源
      最近更新 更多