laa*_*ptu 15 android lowercase uppercase android-edittext
我已经使用以下选项来创建单词大写的每个起始字母
<EditText
android:inputType="text|textCapWords"/>
Run Code Online (Sandbox Code Playgroud)
键入用户时键盘上的选项可以更改字母大小写,即使用此选项的用户可以轻松键入lowercase字母.
此外,我希望我的文字EditText采用这种格式
Each Starting Letter Of A Word Must Be In Uppercase And All Other Letter Of The Word Be In Lowercase.
意思是,当用户输入时
each StArting LeTTer of a word musT be in uppercase and all other leTTer of the word be in lowercase
,它将自动转换为以上格式.
我尝试过使用TextWatcher并string.split(\\s+)获取所有单词,然后使每个单词都遵循上述格式.但我总是得到错误.所以,如果有任何解决方案,那就太棒了.我希望这种方式有效InputFilter.AllCaps.
到目前为止这是我的代码
private void changeToUpperCase(String inputString) {
if (inputString != null && inputString.trim().length() > 0) {
// businessName.addTextChangedListener(null);
String[] splitString = inputString.split("\\s+");
int length = splitString.length;
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
String convertedString = splitString[i];
stringBuffer.append(Character.toUpperCase(convertedString
.charAt(0)));
stringBuffer.append(convertedString.substring(1).toLowerCase());
stringBuffer.append(" ");
}
Log.i("changed String", stringBuffer.toString());
// businessName.setText(stringBuffer.toString());
stringBuffer.delete(0, stringBuffer.length());
stringBuffer = null;
// businessName.addTextChangedListener(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我打电话的这个功能TextWatcher,afterTextChanged(Editable s)
Shy*_*dda 21
在布局中xml,添加android:capitalize="sentences"
选项android:capitalize如下:
android:capitalize="none" :这不会自动大写任何东西.
android:capitalize="sentences" :这将使每个句子的第一个单词大写.
android:capitalize="words" :这将使每个单词的第一个字母大写.
android:capitalize="characters" :这将使每个角色都有资本化.
更新:
由于android:capitalize现在已弃用,需要使用:
android:inputType="textCapWords"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20880 次 |
| 最近记录: |