1

我有一个 EditText,我在其上附加了一个 TextWatcher。每次更改后,内容都会从特殊字符中删除,例如ëä。我放回可编辑的结果以显示在 UI 上。它在所有设备上都能正常工作,三星 Galaxy Note 2 除外。

因此,Note 上发生的情况是第一个字符被接受并显示。一旦输入第二个字符,onTextChanged两者beforeTextChanged都会给出s.length()0

如果您注释掉Selection.setSelection(info, position);它可以工作,但是它将光标设置在开头。

文本观察器

  editTextWatcher = new TextWatcher()
  {

     @Override
     public void onTextChanged(CharSequence s, int start, int before, int count)
     {

     }

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

     }

     @Override
     public void afterTextChanged(Editable s)
     {

          // Prevent a stackoverflow
           editText.removeTextChangedListener(this);

           editText.setText(cleanDescription(s.toString()));
           editText.addTextChangedListener(this);
           editText.requestFocus();

           int position = editText.length();
           Editable info = editText.getText();
           // Set the cursor at the end
           Selection.setSelection(info, position);
     }

  };

清洁功能

private String cleanDescription(String info)
   {
      Log.d(TAG, "cleanDescription()");
      if (info != null)
      {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
         {
            info = Normalizer.normalize(info, Normalizer.Form.NFD);
         }
         info = info.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
         info = info.replaceAll("[^a-zA-Z0-9\\s.,-]", "");
      }
      return info;
   }

Logcat 确实会发出警告

09-23 16:22:24.865: W/IInputConnectionWrapper(5086): setComposingText on inactive InputConnection
09-23 16:22:24.865: W/IInputConnectionWrapper(5086): getExtractedText on inactive InputConnection
09-23 16:22:24.870: W/IInputConnectionWrapper(5086): finishComposingText on inactive InputConnection
4

0 回答 0