이 코드를 사용하면 모든 단어의 첫 문자가 대문자로 표시됩니다.
-EditText보기에 addTextChangedListener를 설정하십시오.
edt_text.addTextChangedListener (감시자);
-TextWatcher 추가
TextWatcher watcher = new TextWatcher() {
int mStart = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String capitalizedText;
if (input.length() < 1)
capitalizedText = input;
else if (input.length() > 1 && input.contains(" ")) {
String fstr = input.substring(0, input.lastIndexOf(" ") + 1);
if (fstr.length() == input.length()) {
capitalizedText = fstr;
} else {
String sstr = input.substring(input.lastIndexOf(" ") + 1);
sstr = sstr.substring(0, 1).toUpperCase() + sstr.substring(1);
capitalizedText = fstr + sstr;
}
} else
capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
if (!capitalizedText.equals(edt_text.getText().toString())) {
edt_text.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) {
edt_text.setSelection(mStart);
edt_text.removeTextChangedListener(this);
}
});
edt_text.setText(capitalizedText);
}
}
};