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

sre*_*ree 22 xml android 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"/>
Run Code Online (Sandbox Code Playgroud)

Chi*_*awa 37

您可以使用此代码.

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());
Run Code Online (Sandbox Code Playgroud)


Pra*_*ena 31

如果有人在寻找kotlin这样做的方式,那么代码变得非常简单和美观.

yourTextView.text = yourText.split(' ').joinToString(" ") { it.capitalize() }
Run Code Online (Sandbox Code Playgroud)


Sil*_*uti 10

试试这个...

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

 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();
}
Run Code Online (Sandbox Code Playgroud)

用法:

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

结果:

Output: Hello Dream World
Run Code Online (Sandbox Code Playgroud)


Int*_*iya 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()
Run Code Online (Sandbox Code Playgroud)

如果你的文字是动态的话

KOTLIN

   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()
Run Code Online (Sandbox Code Playgroud)

  • 现在我只使用Kitkat设备,我也更改了键盘设置,并且我使用了YourTextviewObj.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); 这也是,但没有任何效果 (2认同)
  • 你的链接已经死了 (2认同)

Ank*_*her 5

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

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();
}
Run Code Online (Sandbox Code Playgroud)


Rav*_*avi 4

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

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();
}
Run Code Online (Sandbox Code Playgroud)

如果您想在 xml 中执行此操作,请参考此内容。