TextWatcher をアタッチした EditText があります。ë
変更するたびに、コンテンツからやなどの特殊文字が取り除かれä
ます。編集可能に戻した結果を UI に表示します。Samsung Galaxy Note 2 を除くすべてのデバイスで正常に動作します。
Note で何が起こるかというと、最初の文字が受け入れられて表示されるということです。2 番目の文字がタイプされonTextChanged
、両方がofbeforeTextChanged
を与えると、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