Preface
The official explanation is that setting an attribute on EditText: android:inputType="textCapWords" capitalizes each word, but it turns out that there is no egg to use. I don't know what's going on. (If you know why, please let me know!) Now let's talk about the plan.
programme
Find an answer on Stakoverflow and use TextWatcher to achieve this goal!
Motorola Bionic - input type textPersonName and textCapWords not working
public class WordUtil {
public static String capitalize(String str) {
return capitalize(str, (char[]) null);
}
public static String capitalize(String str, char... delimiters) {
int delimLen = delimiters == null ? -1 : delimiters.length;
if (!TextUtils.isEmpty(str) && delimLen != 0) {
char[] buffer = str.toCharArray();
boolean capitalizeNext = true;
for (int i = 0; i < buffer.length; ++i) {
char ch = buffer[i];
if (isDelimiter(ch, delimiters)) {
capitalizeNext = true;
} else if (capitalizeNext) {
buffer[i] = Character.toTitleCase(ch);
capitalizeNext = false;
}
}
return new String(buffer);
} else {
return str;
}
}
private static boolean isDelimiter(char ch, char[] delimiters) {
if (delimiters == null) {
return Character.isWhitespace(ch);
} else {
char[] arr$ = delimiters;
int len$ = delimiters.length;
for (int i$ = 0; i$ < len$; ++i$) {
char delimiter = arr$[i$];
if (ch == delimiter) {
return true;
}
}
return false;
}
}
}
Then in activity:
TextWatcher capitalizeTW = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try{
String inputString = ""+YOUR_TEXT_VIEW.getText().toString();
String firstLetterCapString = WordUtil.capitalize(inputString);
if(!firstLetterCapString.equals(""+YOUR_TEXT_VIEW.getText().toString())){
YOUR_TEXT_VIEW.setText(""+firstLetterCapString);
YOUR_TEXT_VIEW.setSelection(YOUR_TEXT_VIEW.getText().length());
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
Add TextWatcher for EditText:
YOUR_TEXT_VIEW.addTextChangedListener(capitalizeTW);
For TextView, a similar solution can be used, except that you don't need to set up TextWatcher, and you can simply call the relevant code to manually convert it.
Other relevant
Set all capitals
android:textAllCaps="true" is an attribute that capitalizes all letters for TextView, but it cannot be used for EdidText. IndexOutOfBoundsException is erroneously set in xml and is useless in code. If you want to type all capitals in EditText, you'll think of android: inputType= "tCapacters", but it's useless. It's the same property that doesn't have any eggs to use. So the corresponding solution:
Solution 1: Similarly, we can use the method of adding TextWatcher above to solve the problem.
Scheme 2: Refer to this article: Setting EditText to enter all text in capitals or lowercases
In Button, if there is English in text, it will default to all capitals. We can set textAllCaps to false and make it lowercase!
inputType attribute of EditText
//Text type, mostly capitals, lowercase and numeric numbers.
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters" //Capital letters
android:inputType="textCapWords" //title case
android:inputType="textCapSentences" //Only the first letter is capitalized
android:inputType="textAutoCorrect" //Automatic completion
android:inputType="textAutoComplete" //Automatic completion
android:inputType="textMultiLine" //Multi-line input
android:inputType="textImeMultiLine" //Input method with multiple lines (if supported)
android:inputType="textNoSuggestions" //No prompt
android:inputType="textUri" //Website
android:inputType="textEmailAddress" //E-mail address
android:inputType="textEmailSubject" //Mail Subject
android:inputType="textShortMessage" //SMS
android:inputType="textLongMessage" //Long message
android:inputType="textPersonName" //Name
android:inputType="textPostalAddress" //address
android:inputType="textPassword" //Password
android:inputType="textVisiblePassword" //Visible password
android:inputType="textWebEditText" //Text as a Web Form
android:inputType="textFilter" //Text filtering
android:inputType="textPhonetic" //Pinyin input//numeric type
android:inputType="number"// number
android:inputType="numberSigned" //Symbolized numeric format
android:inputType="numberDecimal" //Floating-point scheme with decimal point
android:inputType="phone" //Dial-up keyboard
android:inputType="datetime" //Time and date
android:inputType="date" //Date keyboard
android:inputType="time" //Time Keyboard