【问题标题】:edit text for name编辑名称的文本
【发布时间】:2012-10-25 12:17:12
【问题描述】:

我有一个经过编辑的文本,将名称作为用户输入。我需要限制除点(。)之外的所有特殊字符。这个怎么做?请参考下面的代码

EditText Name= new EditText(this);
Name.setLayoutParams(new TableRow.LayoutParams(dp(220),dp(40)));
Name.setHorizontallyScrolling(true);        
Name.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
Name.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
Name.setTypeface(Typeface.DEFAULT);
Name.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

【问题讨论】:

标签: android


【解决方案1】:

使用inputType这里是文档的链接参考它

inputType="textPersonName"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType 使用

NameEdt.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 

【讨论】:

  • 我需要限制特殊字符如@ $ / ;等等,但如果我使用 textPersonName ,我会使用 NameEdt.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME|InputType.TYPE_CLASS_TEXT);
【解决方案2】:

试试这个

EditText editText = (EditText)findViewById(R.id.editText);
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter(){
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (end > start) {

                    char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'};

                    for (int index = start; index < end; index++) {                                         
                        if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
                            return ""; 
                        }               
                    }
                }
                return null;
            }

        };
        editText.setFilters(filters);

【讨论】:

  • 非常感谢,看起来很棒
【解决方案3】:
InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter(){
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (end > start) {

                char[] acceptedChars = new char[]{'a','b'};

                for (int index = start; index < end; index++) {                                         
                    if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
                        return ""; 
                    }               
                }
            }
            return null;
        }

    };
    Name.setFilters(filters);

【讨论】:

  • 非常感谢,我遵循了上面的代码,它工作正常
【解决方案4】:

NumberKeyListener PwdkeyListener = new NumberKeyListener() {

public int getInputType() {
return InputType.TYPE_MASK_VARIATION;
}

    @Override
    protected char[] getAcceptedChars() {
    return new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':', 
                    ';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '¢', '•','©' };
    }
};

edtObj.setKeyListener(PwdkeyListener);

更多信息请参见Android - Want to restrict some charaters to the edittext

【讨论】:

  • 非常感谢,我按照上面的代码,它工作正常
猜你喜欢
  • 2015-04-20
  • 2018-10-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多