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

时间:2020-12-28 21:45:22

i need to change the text="font roboto regular" to Font Roboto Regular in xml itself, how to do?

我需要将文本="font roboto regular"改为"font roboto regular"在xml中怎么做?

<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"/>

13 个解决方案

#1


31  

You can use this code.

您可以使用此代码。

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

#2


6  

You can use

您可以使用

android:inputType="textCapSentences"
android:capitalize="sentences"

Go through

经过

https://developer.android.com/reference/android/text/InputType.html

https://developer.android.com/reference/android/text/InputType.html

#3


3  

Try this...

试试这个…

Method that convert first letter of each word in a string into an uppercase letter.

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

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

Usage:

用法:

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

Result:

结果:

Output: Hello Dream World

#4


3  

Modification on the accepted answer to clean out any existing capital letters and prevent the trailing space that the accepted answer leaves behind.

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

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


2  

you can use this method to do it programmatically

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

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

refer this if you want to do it in xml.

如果您想要在xml中进行此操作,请参考它。

#6


1  

android:capitalize is deprecated.

android:利用弃用。

Follow these steps: https://*.com/a/31699306/4409113

遵循以下步骤:https://*.com/a/31699306/4409113

  1. Tap icon of ‘Settings’ on the Home screen of your Android Lollipop Device
  2. 在你的Android棒棒糖设备的主屏幕上点击“设置”图标
  3. At the ‘Settings’ screen, scroll down to the PERSONAL section and tap the ‘Language & input’ section.
  4. 在“Settings”屏幕上,向下滚动到PERSONAL部分,点击“Language & input”部分。
  5. At the ‘Language & input’ section, select your keyboard(which is marked as current keyboard).
  6. 在“语言和输入”部分,选择您的键盘(标记为当前键盘)。
  7. Now tap the ‘Preferences’.
  8. 现在点击“偏好”。
  9. Tap to check the ‘Auto – Capitalization’ to enable it.
  10. 点击查看“自动资本化”以启用它。

And then it should work.

然后它就会工作。

If it didn't, i'd rather to do that in Java.

如果没有,我宁愿用Java来做。

#7


1  

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

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

Have a look at ACL WordUtils.

看看ACL worls。

WordUtils.capitalize("your string") == "Your String"

WordUtils。大写("your string") = "your string"

#8


1  

android:inputType="textCapWords"

This will not work in TextView, which is valid for EditText only. so you have to do it manually.

这在TextView中不起作用,它只对EditText有效。所以你必须手动操作。

You can use capitalizeFully for make each word starts with capital.

你可以使用资本主义,让每个词都以资本开头。

#9


0  

You can use

您可以使用

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

refer this How to capitalize the first character of each word in a string

请参阅如何将字符串中每个单词的第一个字符大写

#10


0  

Another approach is to use StringTokenizer class. The below method works for any number of words in a sentence or in the EditText view. I used this to capitalize the full names field in an app.

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

public String capWordFirstLetter(String fullname)
{
    String fname = "";
    String s2;
    StringTokenizer tokenizer = new StringTokenizer(fullname);
    while (tokenizer.hasMoreTokens())
    {
        s2 = tokenizer.nextToken().toLowerCase();
        if (fname.length() == 0)
            fname += s2.substring(0, 1).toUpperCase() + s2.substring(1);
        else
            fname += " "+s2.substring(0, 1).toUpperCase() + s2.substring(1);
    }

    return fname;
}

#11


0  

You can try these attribute inside your XML tag given by:

您可以在XML标记中尝试以下属性:

To capitalize each word

利用每个单词

android:inputType="textCapWords"

To capitalize each word in start of each sentence:

在每个句子的开头用大写字母表示:

android:inputType="textCapSentences"

While this attribute is deprecated now:

虽然现在不赞成使用此属性:

android:capitalize    **// deprecated X**

#12


0  

To capitalize each word in a sentence use the below attribute in xml of that paticular textView.

要将一个句子中的每个单词都大写,可以使用下面的属性,在这个patitextview的xml中。

android:inputType="textCapWords"

android:inputType = " textCapWords "

#13


0  

If someone looking for kotlin way of doing this, then code becomes very simple and beautiful.

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

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

#1


31  

You can use this code.

您可以使用此代码。

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

#2


6  

You can use

您可以使用

android:inputType="textCapSentences"
android:capitalize="sentences"

Go through

经过

https://developer.android.com/reference/android/text/InputType.html

https://developer.android.com/reference/android/text/InputType.html

#3


3  

Try this...

试试这个…

Method that convert first letter of each word in a string into an uppercase letter.

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

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

Usage:

用法:

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

Result:

结果:

Output: Hello Dream World

#4


3  

Modification on the accepted answer to clean out any existing capital letters and prevent the trailing space that the accepted answer leaves behind.

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

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


2  

you can use this method to do it programmatically

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

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

refer this if you want to do it in xml.

如果您想要在xml中进行此操作,请参考它。

#6


1  

android:capitalize is deprecated.

android:利用弃用。

Follow these steps: https://*.com/a/31699306/4409113

遵循以下步骤:https://*.com/a/31699306/4409113

  1. Tap icon of ‘Settings’ on the Home screen of your Android Lollipop Device
  2. 在你的Android棒棒糖设备的主屏幕上点击“设置”图标
  3. At the ‘Settings’ screen, scroll down to the PERSONAL section and tap the ‘Language & input’ section.
  4. 在“Settings”屏幕上,向下滚动到PERSONAL部分,点击“Language & input”部分。
  5. At the ‘Language & input’ section, select your keyboard(which is marked as current keyboard).
  6. 在“语言和输入”部分,选择您的键盘(标记为当前键盘)。
  7. Now tap the ‘Preferences’.
  8. 现在点击“偏好”。
  9. Tap to check the ‘Auto – Capitalization’ to enable it.
  10. 点击查看“自动资本化”以启用它。

And then it should work.

然后它就会工作。

If it didn't, i'd rather to do that in Java.

如果没有,我宁愿用Java来做。

#7


1  

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

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

Have a look at ACL WordUtils.

看看ACL worls。

WordUtils.capitalize("your string") == "Your String"

WordUtils。大写("your string") = "your string"

#8


1  

android:inputType="textCapWords"

This will not work in TextView, which is valid for EditText only. so you have to do it manually.

这在TextView中不起作用,它只对EditText有效。所以你必须手动操作。

You can use capitalizeFully for make each word starts with capital.

你可以使用资本主义,让每个词都以资本开头。

#9


0  

You can use

您可以使用

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

refer this How to capitalize the first character of each word in a string

请参阅如何将字符串中每个单词的第一个字符大写

#10


0  

Another approach is to use StringTokenizer class. The below method works for any number of words in a sentence or in the EditText view. I used this to capitalize the full names field in an app.

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

public String capWordFirstLetter(String fullname)
{
    String fname = "";
    String s2;
    StringTokenizer tokenizer = new StringTokenizer(fullname);
    while (tokenizer.hasMoreTokens())
    {
        s2 = tokenizer.nextToken().toLowerCase();
        if (fname.length() == 0)
            fname += s2.substring(0, 1).toUpperCase() + s2.substring(1);
        else
            fname += " "+s2.substring(0, 1).toUpperCase() + s2.substring(1);
    }

    return fname;
}

#11


0  

You can try these attribute inside your XML tag given by:

您可以在XML标记中尝试以下属性:

To capitalize each word

利用每个单词

android:inputType="textCapWords"

To capitalize each word in start of each sentence:

在每个句子的开头用大写字母表示:

android:inputType="textCapSentences"

While this attribute is deprecated now:

虽然现在不赞成使用此属性:

android:capitalize    **// deprecated X**

#12


0  

To capitalize each word in a sentence use the below attribute in xml of that paticular textView.

要将一个句子中的每个单词都大写,可以使用下面的属性,在这个patitextview的xml中。

android:inputType="textCapWords"

android:inputType = " textCapWords "

#13


0  

If someone looking for kotlin way of doing this, then code becomes very simple and beautiful.

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

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