私は以下の機能を使用して、各単語の最初の文字をEdittextの大文字に強制します。EditTextカーソル位置付け問題
public String capitalizeFirstLetterWord(String s) {
StringBuilder cap = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
try {
char x = s.charAt(i);
if (x == ' ') {
cap.append(" ");
char y = s.charAt(i + 1);
cap.append(Character.toUpperCase(y));
i++;
} else {
cap.append(x);
}
} catch (IndexOutOfBoundsException ignored) {
}
}
//finally, capitalize the first letter of the sentence
String sentence = cap.toString();
if (sentence.length() > 0) {
sentence = String.valueOf(sentence.charAt(0)).toUpperCase(); //capitalize first letter
if (cap.toString().length() > 1) { //check if there's succeeding letters
sentence += cap.toString().substring(1); //append it also
}
}
return sentence;
}
と以下のようにafterTextChangeでそれを呼び出す()メソッド:
@Override
public void afterTextChanged(Editable editable) {
if (getActivity().getCurrentFocus() == mEdtName) {
if (editable.toString().length() > 0 &&
!editable.toString().equals(mOldName)) {
mOldName = editable.toString(); //prevent infinite loop
mEdtName.setText(capitalizeFirstLetterWord(editable.toString()));
mEdName.setSelection(mEdGymName.getText().length()); //set the cursor to the end of the editText
}
}
}
しかし、私はEditextで、文字列全体の途中から文字を消去しようとすると、問題があります。カーソルはテキストの最後に移動しています。 。 afterTextChanged()メソッドの下の行が原因です。 その行にコメントすると、カーソルが最初の位置に移動します。 。
解決方法はありますか?
mEditName.addTextChangedListener(this);を追加しました。 ???出力の変更はありません: –
@JaiminModiいいえ、6行目を見てくださいmEditName.removeTextChangedListener(this); –
しかし、まだ動作しません。コピーしました。 –