如何为自定义TextView的第一个字母大写?

4

在自定义的TextView控件中,如果第一个字符是数字,则下一个字符将是字符。如何找到其中的第一个数字字符。


可能是Java:查找第一个Regex的索引的重复。 - N J
5个回答

9
如果您使用Kotlin,可以选择以下方式进行操作:
首字母大写:
var str = "whaever your string is..."
str.capitalize()
// Whaever your string is...

每个单词首字母大写

var str = "whaever your string is..."
val space = " "
val splitedStr = str.split(space)
str = splitedStr.joinToString (space){
    it.capitalize()
}
// Whaever Your String Is...

6
尝试通过拆分整个单词来使用此方法。
String input= "sentence";
String output = input.substring(0, 1).toUpperCase() + input.substring(1);
textview.setText(output);

输出: 句子

5
您需要在TextView的xml布局文件中查找inputType参数。在您希望以驼峰命名方式设置TextView的布局文件中,添加以下行:

android:inputType = "textCapWords"
//This would capitalise the first letter in every word.

如果你只想让 TextView 的第一个字母大写,可以使用以下方法。
android:inputType = "textCapSentences"
//This would capitalise the first letter in every sentence.

如果你有一个包含多个句子的textView,并且你只想将TextView中的第一个字母大写,我建议使用以下代码实现:
String[] words = input.getText().toString().split(" ");
StringBuilder sb = new StringBuilder();
if (words[0].length() > 0) {
    sb.append(Character.toUpperCase(words[0].charAt(0)) + words[0].subSequence(1, words[0].length()).toString().toLowerCase());
    for (int i = 1; i < words.length; i++) {
        sb.append(" ");
        sb.append(Character.toUpperCase(words[i].charAt(0)) + words[i].subSequence(1, words[i].length()).toString().toLowerCase());
    }
}
String titleCaseValue = sb.toString();

希望这能帮到您:)

0
使用此函数传递您的字符串并返回大写字符串。
public static String wordCapitalize(String words)
{

    String str = "";
    boolean isCap = false;

    for(int i = 0; i < words.length(); i++){

        if(isCap){
            str +=  words.toUpperCase().charAt(i);
        }else{
            if(i==0){
                str +=  words.toUpperCase().charAt(i);
            }else {
                str += words.toLowerCase().charAt(i);
            }
        }

        if(words.charAt(i)==' '){
            Utility.debug(1,TAG,"Value of  i : "+i+" : "+words.charAt(i)+" : true");
            isCap = true;
        }else{
            Utility.debug(1,TAG,"Value of  i : "+i+" : "+words.charAt(i)+" : false");
            isCap = false;
        }
    }
    Utility.debug(1,TAG,"Result : "+str);
    return str;
}

-2
String text = textView.getText().toString();
for(Character c : text){
   if(c.isLetter){
     //First letter found
   break;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接