我需要自动将化合物名称中的第一个字母大写,例如:Gay-Lussac(法国物理学家),Иванов-Вано(俄罗斯动画导演)。
Android EditText仅以全名首字母大写,我得到了Gay-lussac和Иванов-вано。
有什么方法吗?
我的EditText是
<EditText
android:id="@+id/nameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="31dp"
android:hint="@string/hint_name"
android:imeOptions="actionNext"
android:inputType="textCapWords|textCapSentences|textPersonName"
android:singleLine="true" />
我也试过(也不适用于第二部分):
<EditText
...
android:capitalize="words"
android:singleLine="true" />
和
<EditText
...
android:capitalize="words"
android:inputType="textCapWords|textCapSentences"
android:singleLine="true" />
答案 0 :(得分:0)
您可以分两步操作:
1 - 在strings.xml中让你的字符串间隔(即:"Gay - Lussac"
),让Android将它们大写。
2 - 然后用Java中的" - "
替换"-"
。
<强> [编辑] 强>
如果输入来自用户,则替换步骤 1 步骤:
1 - 首先,将用户输入字符串"-"
替换为" - "
答案 1 :(得分:0)
您应该创建一个包含静态方法的类:
public static CharSequence capitalize(CharSequence name){
if(name == null) return null;
String sName = name.toString().toLowerCase().trim();
String result = "";
sName = capFirst(sName);
if(sName.contains("-")){
result = "";
String[] subParts = sName.split("-");
for(int i = 0; i < subParts.length; i++){
String subPart = subParts[i];
result += capFirst(subPart);
if(i < subParts.length - 1) result += "-";
}
}
else result = sName;
return result.trim();
}
private static String capFirst(String text){
if(text == null) return null;
text = text.toLowerCase().trim();
String result = "";
if(text.length() > 0){
text = text.substring(0, 1).toUpperCase() + text.substring(1);
if(text.contains(" ")){
for(String word : text.split(" ")){
word = word.trim();
if(word.length() > 0) result += " " + word.substring(0, 1).toUpperCase() + word.substring(1);
}
}
else result = text;
}
return result.trim();
}
然后为EditText的适当事件添加一个监听器,并使用YourClass.capitalize()设置它的文本。
答案 2 :(得分:0)
我自己做了大写。 如果此字符是字符串中的最后一个字符并且它位于任何非字母字符之后(或者它是字符串中的单个字符),则它将最后输入的字符大写。
public class EditName extends EditText {
public EditName(Context context, AttributeSet attrs) {
super(context, attrs);
addTextChangedListener(new TextWatcher() {
String lastChanged = "";
boolean isLastSingleCharChanged = false;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// check if only last character was changed
isLastSingleCharChanged =
count - before == 1 // was added one character
&& start + count == s.length() // last character was in changed group
&& (start == 0 || count == 1); // or total change, or last character
}
@Override
public void afterTextChanged(Editable s) {
if (!isLastSingleCharChanged) return; // only if last character changed (see above)
int len = s.length();
if (len == 0) return;
if (len > 1 && Character.isLetter(s.charAt(len - 2))) return; // correct letter only after none-letter
char c = s.charAt(len - 1);
if (!Character.isLetter(c)) return;
if (!Character.isLowerCase(c)) return;
String text = s.toString();
if (text.equals(lastChanged)) return; // do not do the same correction twice
lastChanged = text;
EditName.this.setText(text.substring(0, text.length() - 1)
+ Character.toUpperCase(c));
EditName.this.setSelection(text.length()); // position cursor
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
}
}