编辑名称的文本

时间:2012-10-25 12:17:13

标签: android

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

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()});

4 个答案:

答案 0 :(得分:3)

使用inputType这里是doc refer it

的链接
inputType="textPersonName"

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

NameEdt.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 

答案 1 :(得分: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);

答案 2 :(得分:1)

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);

答案 3 :(得分:1)

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

,请参阅此处