在android中输入edittext时删除特殊字符;

时间:2012-10-23 07:04:11

标签: android android-edittext

我需要删除字符,如果我向编辑文本添加任何特殊符号,同时输入数据到它。例如我键入一个单词smwinææ是一个特殊字符所以如果我添加chareter编辑文本应该删除æ并通过替换æ.i使用的文本更改侦听器仅显示smwin。检查下面的代码

email.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
            System.out.println("started after");
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            System.out.println("started");

            // Abstract Method of TextWatcher Interface.
        }

        public void onTextChanged(CharSequence searchcontact, int start,
                int before, int count) {
            System.out.println("sssssssssss"+searchcontact);
            String a=searchcontact.toString();
            System.out.println("casted "+a);
            String[] parts = a.split(" ");
            String lastWord = parts[parts.length - 1];
            System.out.println("------------------------------"+lastWord);

//              String lastWord = a.substring(a.lastIndexOf(" ")+1);
//              System.out.println("lastword"+lastWord);
            if(a.equals("p"))
            {

            try {  
                email.getText().delete(email.getSelectionEnd() - 1, email.getSelectionStart());  
            } catch (Exception e) {  
                try {  
                    email.getText().delete(email.length() - 1, email.length());  
                } catch (Exception myException) {  
                //textfield.getText().delete(textfield.length(), textfield.length() - 1);  
                }  
            }  
            // Method User For Sort Out THE Words
                                        // in
                                        // The SearchBar
            }
        }

    });

这里当文本改变时我试图获取字符串中的最后一个单词但我没有做到这一点。使用

  

String a = searchcontact.toString();                   System.out.println(“casted”+ a);                   String [] parts = a.split(“”);                   String lastWord = parts [parts.length - 1];

得到最后一个字但它在edittext中打印整个字符串我该怎么做呢请帮助

1 个答案:

答案 0 :(得分:1)

您可以将TextWatcher添加到EditText,并在每次通知文本时收到通知。使用它,您可以解析String以查找空格后的字符,并将它们更新为大写。

这是我做的快速测试,效果非常好(远非最佳,因为每次编辑Editable时,它都会再次调用侦听器......)。

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        String string = s.toString();
        int index = -1;
        while (((index = string.indexOf(' ', index + 1)) != -1) && (index + 1 < string.length())) {
            // Get character
            char c = string.charAt(index + 1);
            if (Character.isLowerCase(c)) {

                // Replace in editable by uppercase version
                s.replace(index+1, index + 2, Character.toString(c).toUpperCase());
            }
        }
    }
});

为了避免经常被调用,您可以在char []中进行所有更改,并且只有在进行了更改时才提交给Editable。

一个更简单的解决方案可能只是在你的String上使用split(''),用大写版本(如果需要)替换String []中的所有第一个字母,并只对可编辑提交一次。

更简单的优化是将匿名类添加到布尔值,将其设置为在输入afterTextChanged时尝试,在退出时将其设置为false,并且仅在布尔值为false时处理字符串。