1

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

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
            }
        }

    });

在这里,当文本更改时,我尝试获取字符串中的最后一个单词,但我没有这样做。使用

字符串 a=searchcontact.toString(); System.out.println("铸造的"+a); String[] 部分 = a.split(" "); 字符串 lastWord = parts[parts.length - 1];

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

4

1 回答 1

1

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

这是我做的一个快速测试,效果很好(远非最佳,因为每次编辑 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。

一个更简单的解决方案可能是在您的字符串上使用 split(' '),将 String[] 中的所有首字母替换为大写版本(如果需要),并且只提交一次到 Editable。

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

于 2012-10-23T07:09:26.573 回答