键入时在编辑文本中大写每个单词 [英] Capitalize every word in Edit text while typing
问题描述
我想在键入文本时将编辑文本中的每个单词首字母大写.
I want to capitilze every word in edit text while typing.
我的XML:-
<com.project.edittext.AutoFitEditText
android:id="@+id/txt_name"
style="@style/scan_text_fields_white_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="@string/validation_accept_all_except_special_char"
android:fontFamily="sans-serif"
android:imeOptions="flagNoExtractUi|actionDone"
android:inputType="textNoSuggestions|textCapWords"
android:maxLength="50"
android:paddingBottom="3dp"
android:paddingLeft="12dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:singleLine="true"
android:textColor="@color/orange"
/>
现在我正在使用
mName.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
但是问题是它不能在某些设备上运行.前LG平板电脑.因此,我决定以编程方式进行此操作.
所以我正在使用以下代码.
So I am using following codes.
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String mNameStr=s.toString();
String finalStr="";
if(!mNameStr.equals("")) {
String[] strArray = mNameStr.split("[\\s']");
if (strArray.length > 0) {
for (int i = 0; i < strArray.length; i++) {
finalStr+=capitalize(strArray[i]);
}
Logger.d("finalStr==> ", finalStr);
}
}
mName.setText(finalStr.toString());
}
private String capitalize(final String line) {
return Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
这里我的应用崩溃了.我在调试时发现了问题.问题是onTextChanged没有停止.使它连续执行.任何人都可以帮助我解决我的问题.我想把每个词都大写.请提出任何解决方案.
Here my app getting crash. I found the problem while debugging. Problem is the onTextChanged is not getting stopped. So that it execute continuously. Anyone help me to resolve my problem. I want to capitalize every word. Please suggest any solutions.
推荐答案
代码冻结的原因是onChangeListener
会在您setText
时立即触发.
The reason your code freezes is that the onChangeListener
is triggered as soon as you setText
.
如果文本相同,则必须找到一种停止触发的方法.
You'll have to find a way to stop trigering if the text is the same.
为此,您可以使用临时字符串存储prev值,并在每次事件触发时进行比较.这样可以使流程保持一致且不会循环.
For this you can use a temp string to store the prev value and compare each time the event triggers. This will keep the flow to be consistent and not looping.
String prevString = "";
在afterTextChanged
String mNameStr = s.toString();
String finalStr = "";
if (!mNameStr.equals("")) {
String[] strArray = mNameStr.split("[\\s']");
if (strArray.length > 0) {
int i = 0;
for (i = 0; i < strArray.length - 1; i++) {
finalStr += capitalize(strArray[i]) + " ";
}
finalStr += capitalize(strArray[i]);
Log.d("finalStr==> ", finalStr);
}
}
if (!finalStr.equals(prevString)) {
prevString = finalStr;
editText.setText(finalStr.toString());
editText.setSelection(finalStr.length());
}
对代码的优化是仅在最后输入的字母位于空格之后才将句子大写.您可以在此处看到它的实现.
An optimization to your code is to only capitalize the sentence if the last entered letter is after a space. You can see it implemented here.
editText.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) {
String string = s.toString();
Log.d("STRING", string + " " + prevString);
if (string.equals(prevString)) {
return;
} else if (string.length() == 0)
return;
// 1st character
else if (string.length() == 1) {
prevString = string.toUpperCase();
editText.setText(string.toUpperCase());
editText.setSelection(string.length());
}
// if the last entered character is after a space
else if (string.length() > 0 && string.charAt(string.length() - 2) == ' ') {
string = string.substring(0, string.length() - 1) + Character.toUpperCase(string.charAt(string.length() - 1));
prevString = string;
editText.setText(string);
editText.setSelection(string.length());
}
}
});
但是,上述方法有一个缺点.这不会考虑您更改光标位置并输入文本的情况.
However, there is a down-side to the above method. This will not consider the case where in you change the cursor position and enter text.
PS:您可以使用getSelectionEnd()
方法并增强上述方法
PS: you can use getSelectionEnd()
method and enhance the above method
这篇关于键入时在编辑文本中大写每个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!