Android版的EditText / TextView中如何使每个单词开始与大写和单词的所有剩余的字符是小写单词、剩余、字符、Android

2023-09-07 17:07:39 作者:鹤川.

我已经使用下列选项做出字的每首字母大写

I have already used following options to make each starting letter of a word Uppercase

 <EditText
    android:inputType="text|textCapWords"/>

虽然键入该用户在键盘上的选项更改信件的情况下,即使用此选项,用户可以轻松地输入小写字母。

此外,我想在我的文字的EditText 要上这种格式

Further,I want text on my EditText to be on this format

开始的每一个字必须是大写和所有其他字母词的使用小写

的含义,当用户输入

A字的每首字母必须大写字母,单词的所有其他字母为小写

,它会自动转换成以上格式。

, it will be automatically converted to above format.

我已经尝试使用 TextWatcher string.split(\\\\ S +)来让所有的单词,然后使每一位单词按照上面的格式。但是,我总是最后得到错误。所以,如果有任何解决方案,这将是great.I希望这种的方式工作 InputFilter.AllCaps

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.

这是我的code到目前为止

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);
    }
}

这个功能我从 TextWatcher afterTextChanged致电(编辑S)

推荐答案

在布局 XML 添加机器人:利用=句子

有关的选项的android:利用的以下内容:

The options for android:capitalize are following :

机器人:利用=无:这不会自动大写什么

android:capitalize="none" : which won't automatically capitalize anything.

机器人:利用=语句:这将利用每句的第一个字

android:capitalize="sentences" : which will capitalize the first word of each sentence.

的android:大写=字:这将利用每个单词的第一个字母

android:capitalize="words" : which will capitalize the first letter of every word.

机器人:利用=字符:这将利用每个字符