Android EditText/TextView 如何让每个单词以大写开头,单词的所有剩余字符都为小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21155111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android EditText/TextView how to make each word start with uppercase and all remaining characters of words to be lowercase
提问by laaptu
I have already used following options to make each starting letter of a word Uppercase
我已经使用以下选项使单词的每个起始字母大写
<EditText
android:inputType="text|textCapWords"/>
While typing the user has option on the keyboard to change the case of letter i.e. the user with this option can easily type lowercase
letters.
键入时,用户可以在键盘上选择更改字母的大小写,即具有此选项的用户可以轻松键入lowercase
字母。
Further,I want text on my EditText
to be on this format
此外,我希望我的文字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
.
Meaning,when the user inputs
意思是,当用户输入
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
, it will be automatically converted to above format.
,它将自动转换为上述格式。
I have tried using TextWatcher
and string.split(\\s+)
to get all the words and then make each and every word to follow the above format. But I always end up getting error.
So if there is any solution,it would be great.I want this to work in the manner InputFilter.AllCaps
.
我尝试使用TextWatcher
和string.split(\\s+)
获取所有单词,然后使每个单词都遵循上述格式。但我总是最终得到错误。所以如果有任何解决方案,那就太好了。我希望它以这种方式工作InputFilter.AllCaps
。
This is my code so far
到目前为止,这是我的代码
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);
}
}
This function I am calling from TextWatcher
, afterTextChanged(Editable s)
我正在调用的这个函数TextWatcher
,afterTextChanged(Editable s)
回答by Shylendra Madda
In the layout xml
, add android:capitalize="sentences"
在布局中xml
,添加android:capitalize="sentences"
The options for android:capitalize
are following :
的选项android:capitalize
如下:
android:capitalize="none"
: which won't automatically capitalize anything.
android:capitalize="none"
: 这不会自动大写任何内容。
android:capitalize="sentences"
: which will capitalize the first word of each sentence.
android:capitalize="sentences"
: 这将使每个句子的第一个单词大写。
android:capitalize="words"
: which will capitalize the first letter of every word.
android:capitalize="words"
: 将大写每个单词的第一个字母。
android:capitalize="characters"
: which will capitalize every character.
android:capitalize="characters"
: 将大写每个字符。
Update:
更新:
As android:capitalize
is deprecated now need to use:
由于android:capitalize
已弃用,现在需要使用:
android:inputType="textCapWords"
回答by Shubham
change your input type programmatically.
以编程方式更改您的输入类型。
If you are in View layout than use this code
如果您在视图布局中使用此代码
EditText text = new EditText(context);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
addView(text);
and if you are in Activity
如果你在活动中
EditText text = new EditText(this);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
setContentView(text);
回答by Jaydipsinh Zala
Try this,
尝试这个,
txtView.setText(WordUtils.capitalize("text view")
WordUtils.java
WordUtils.java
public class WordUtils {
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;
}
}
}?
回答by Principiante
android:capitalize
is deprecated. Use inputType
instead.
android:capitalize
已弃用。使用inputType
来代替。
回答by Suraj Vaishnav
To make first letter capital of every word:
使每个单词的首字母大写:
android:inputType="textCapWords"
To make first letter capital of every sentence:
使每个句子的首字母大写:
android:inputType="textCapSentences"
To make every lettercapital:
使每个字母大写:
android:inputType="textCapCharacters"