如何大写自定义textview中的第一个字母?

在自定义TextView中,假设第一个字符是数字,那么下一个字符就是一个字符。 如何find第一个字符amoung数字。

在布局xml中,添加android:capitalize =“句子”

android:capitalize的选项如下:

android:capitalize =“none” :它不会自动大写任何东西。

android:capitalize =“句子” :这将大写每个句子的第一个单词。

android:capitalize =“words” :这将大写每个单词的第一个字母。

android:大写=“字符” :这将大写每个字符。

更新:

由于android:capitalize已经被弃用,现在需要使用:

安卓的inputType = “textCapWords”

您正在寻找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(); 

希望这可以帮助 :)

尝试通过分割整个单词的方法

 String input= "sentence"; String output = input.substring(0, 1).toUpperCase() + input.substring(1); textview.setText(output); 

输出:句子

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

如果你正在使用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...