【问题标题】:TextWatcher onTextChanged not working with soft keyboard auto-complete / suggested wordsTextWatcher onTextChanged 不适用于软键盘自动完成/建议的单词
【发布时间】:2016-01-02 12:44:05
【问题描述】:

我在 EditText 上实现了一个 TextWatcher,以便在用户输入新字符时在文本中查找和下划线一系列关键字。 However when a suggested / auto-complete word on the soft keyboard is selected, instead of adding the suggested word to the Edittext and then calling the onTextChanged function the half complete word is deleted.我发现这很奇怪,因为输入单个字符会很好地激活 onTextChanged 函数。任何帮助将不胜感激。

PS。如果有人知道动态处理 EditText 的更好方法,请告诉我。

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_codify_test);
    final EditText editText = (EditText) findViewById(R.id.editText_codifyTest);
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!checked) { //stop infinite loop
                checked = true;
                cursorPosition = editText.getSelectionStart(); //get cursor position before text modification
                codifyText(editText);
            } else {
                checked = false;
            }
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

//Find and underline keywords
private void codifyText(EditText editText) {
    String plainText = editText.getText().toString() + " ";
    int prevWhiteSpace = 0;
    final Context context = this;
    SpannableString codifiedText = new SpannableString(plainText.substring(0, plainText.length() - 1));
    if (codifiedText.length() == 0) return;

    for (int i = 0; i < plainText.length(); i ++){
        if (Character.isWhitespace(plainText.charAt(i))){
            String currWord = plainText.substring(prevWhiteSpace, i);
            if (isKeyWordInDataBase(currWord)) {
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View view) {

                    }
                };
            codifiedText.setSpan(clickableSpan, prevWhiteSpace, i, 0);
            }
            prevWhiteSpace = i + 1;
        }
    }
    editText.setMovementMethod(LinkMovementMethod.getInstance());
    editText.setText(codifiedText, TextView.BufferType.SPANNABLE);
    editText.setSelection(cursorPosition); //set cursor to position prior to edit
}

【问题讨论】:

    标签: android android-edittext android-softkeyboard textwatcher android-textwatcher


    【解决方案1】:

    最好使用 afterTextChanged 方法。
    在以下示例中,使用 Handler 检索和处理输入的文本以进行进一步处理

        EditText text1;
        StringBuffer previousChar=new StringBuffer();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        text1=(EditText)findViewById(R.id.editText);
        text1.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
                        if(!previousChar.toString().equals(s.toString())){
                            Message msg=new Message();
                            msg.obj=s.toString();
                            localHandler.sendMessage(msg);
                            previousChar=new StringBuffer(s.toString());
                            Log.i("TAG", "TextEntered = "+s);
                        }
                    }
                });}
    

    在处理程序中

    private Handler localHandler = new Handler(){
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String value=(String)msg.obj;
                //Your logic with the text entered that is retrieved 
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2015-07-29
      • 1970-01-01
      相关资源
      最近更新 更多