如何在Textview xml中将每个单词的第一个字母更改为大写

时间:2016-02-03 12:56:31

标签: android xml android-layout

我需要在xml本身中将 text =“font roboto regular”更改为 Font Roboto Regular ,该怎么办?

<TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:textSize="18sp"
   android:textColor="@android:color/black"
   android:fontFamily="roboto-regular"
   android:text="font roboto regular"
   android:inputType="textCapWords"
   android:capitalize="words"/>

15 个答案:

答案 0 :(得分:36)

您可以使用此代码。

String str = "font roboto regular";
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
for (String s : strArray) {
     String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
     builder.append(cap + " ");
}
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(builder.toString());

答案 1 :(得分:17)

如果有人在寻找kotlin这样做的方法,那么代码变得非常简单和漂亮。

yourTextView.text = yourText.split(' ').joinToString(" ") { it.capitalize() }

答案 2 :(得分:8)

试试这个......

将字符串中每个单词的第一个字母转换为大写字母的方法。

 private String capitalize(String capString){
    StringBuffer capBuffer = new StringBuffer();
    Matcher capMatcher = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(capString);
    while (capMatcher.find()){
        capMatcher.appendReplacement(capBuffer, capMatcher.group(1).toUpperCase() + capMatcher.group(2).toLowerCase());
    }

    return capMatcher.appendTail(capBuffer).toString();
}

用法:

String chars = capitalize("hello dream world");
//textView.setText(chars);
System.out.println("Output: "+chars);

结果:

Output: Hello Dream World

答案 3 :(得分:6)

<强> 科特林

   val strArrayOBJ = "Your String".split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                val builder = StringBuilder()
                for (s in strArrayOBJ) {
                    val cap = s.substring(0, 1).toUpperCase() + s.substring(1)
                    builder.append("$cap ")
                }
txt_OBJ.text=builder.toString()

答案 4 :(得分:4)

修改已接受的答案,以清除任何现有的大写字母,并防止接受的答案留下的尾随空格。

public static String capitalize(@NonNull String input) {

    String[] words = input.toLowerCase().split(" ");
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < words.length; i++) {
        String word = words[i];

        if (i > 0 && word.length() > 0) {
            builder.append(" ");
        }

        String cap = word.substring(0, 1).toUpperCase() + word.substring(1);
        builder.append(cap);
    }
    return builder.toString();
}

答案 5 :(得分:3)

您可以使用此方法以编程方式执行此操作

public String wordFirstCap(String str)
{
    String[] words = str.trim().split(" ");
    StringBuilder ret = new StringBuilder();
    for(int i = 0; i < words.length; i++)
    {
        if(words[i].trim().length() > 0)
        {
            Log.e("words[i].trim",""+words[i].trim().charAt(0));
            ret.append(Character.toUpperCase(words[i].trim().charAt(0)));
            ret.append(words[i].trim().substring(1));
            if(i < words.length - 1) {
                ret.append(' ');
            }
        }
    }

    return ret.toString();
}

如果您想在xml中执行此操作,请参阅this

答案 6 :(得分:2)

首字母大写

android:inputType="textCapWords"

大写第一句话:

android:inputType="textCapSentences"

答案 7 :(得分:1)

android:inputType="textCapWords"

这不适用于TextView,仅对EditText有效。所以你必须手动完成。

您可以使用capitalizeFully使每个单词以大写字母开头。

答案 8 :(得分:1)

function onEdit(e){ var s = e.source; var row = e.range.getRow(); var fCell = s.getRange('F'+row); var fValue = fCell.getValue(); Spreadsheet.getActive().toast(fValue); } 已被弃用。

请按以下步骤操作: https://stackoverflow.com/a/31699306/4409113

  
      
  1. 点按Android Lollipop主屏幕上的“设置”图标   设备
  2.   
  3. 在“设置”屏幕上,向下滚动到“个人”部分和   点击'语言&amp;输入'部分。
  4.   
  5. 在'语言&amp;输入'部分,选择你的键盘(即   标记为当前键盘。)
  6.   
  7. 现在点按“偏好设置”。
  8.   
  9. 点按以查看“自动资本化”以启用它。
  10.   

然后它应该有用。

如果没有,我宁愿在android:capitalize中这样做。

答案 9 :(得分:1)

https://stackoverflow.com/a/1149869/2725203

查看ACL WordUtils。

WordUtils.capitalize(“your string”)==“Your String”

答案 10 :(得分:0)

您可以使用

private String capitalize(final String line) {
   return Character.toUpperCase(line.charAt(0)) + line.substring(1);
}

参考此How to capitalize the first character of each word in a string

答案 11 :(得分:0)

另一种方法是使用frameCount类。以下方法适用于句子或EditText视图中的任意数量的单词。我用它来大写应用程序中的全名字段。

StringTokenizer

答案 12 :(得分:0)

要将句子中的每个单词大写,请使用该特定textView的xml中的以下属性。

  

机器人:的inputType = “textCapWords”

答案 13 :(得分:0)

在 kotlin 中,字符串扩展

fun String?.capitalizeText() = (this?.toLowerCase(Locale.getDefault())?.split(" ")?.joinToString(" ") { if (it.length <= 1) it else it.capitalize(Locale.getDefault()) }?.trimEnd())?.trim()

答案 14 :(得分:0)

Kotlin 扩展函数,每个单词大写

val String?.capitalizeEachWord
    get() = (this?.lowercase(Locale.getDefault())?.split(" ")?.joinToString(" ") {
        if (it.length <= 1) it else it.replaceFirstChar { firstChar ->
            if (firstChar.isLowerCase()) firstChar.titlecase(
                Locale.getDefault()
            ) else firstChar.toString()
        }
    }?.trimEnd())?.trim()
相关问题