如何在 Android 应用程序中将 TextView 中文本的第一个字母大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3419521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:06:52  来源:igfitidea点击:

How to capitalize the first letter of text in a TextView in an Android Application

androidxml

提问by Allen Gingrich

I'm not referring to textInput, either. I mean that once you have static text in a TextView (populated from a Database call to user inputted data (that may not be Capitalized)), how can I make sure they are capitalized?

我也不是指 textInput。我的意思是,一旦你在 TextView 中有静态文本(从数据库调用填充到用户输入的数据(可能不是大写)),我如何确保它们是大写的?

Thanks!

谢谢!

回答by Cheryl Simon

I should be able to accomplish this through standard java string manipulation, nothing Android or TextView specific.

我应该能够通过标准的 java 字符串操作来实现这一点,而不是特定于 Android 或 TextView。

Something like:

就像是:

String upperString = myString.substring(0, 1).toUpperCase() + myString.substring(1).toLowerCase();

Although there are probably a million ways to accomplish this. See Stringdocumentation.

尽管可能有一百万种方法可以实现这一目标。请参阅字符串文档。

EDITEDI added the .toLowerCase()

编辑我添加了.toLowerCase()

回答by Beto Caldas

Following does not apply to TextView, but works with EditText; even then, it applies to the text entered from the keyboard, not the text loaded with setText(). To be more specific, it turns the Caps on in the keyboard, and the user can override this at her will.

以下不适用于 TextView,但适用于 EditText;即便如此,它也适用于从键盘输入的文本,而不是使用 setText() 加载的文本。更具体地说,它会打开键盘上的 Caps,用户可以随意覆盖它。

android:inputType="textCapSentences"

or

或者

TV.sname.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);


This will CAP the first letter.

这将大写第一个字母。

or

或者

compile 'org.apache.commons:commons-lang3:3.4' //in build.gradle module(app)

tv.setText(StringUtils.capitalize(myString.toLowerCase().trim()));

回答by noloman

StringBuilder sb = new StringBuilder(name);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));  
return sb.toString();

回答by Ben Kax

for Kotlin, just call

对于 Kotlin,只需调用

textview.text = string.capitalize()

回答by Prakash

You can add Apache Commons Langin Gradle like compile 'org.apache.commons:commons-lang3:3.4'

您可以 在 Gradle 中添加Apache Commons Lang,例如compile 'org.apache.commons:commons-lang3:3.4'

And use WordUtils.capitalizeFully(name)

并使用 WordUtils.capitalizeFully(name)

回答by Androiderson

For future visitors, you can also (best IMHO) import WordUtilfrom Apacheand add a lot of useful methods to you app, like capitalizeas shown here:

对于未来的访问者,您还可以(最好的恕我直言)WordUtilApache应用程序导入并向您的应用程序添加许多有用的方法,如下capitalize所示:

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

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

回答by Sidharth V

The accepted answer is good, but if you are using it to get values from a textView in android, it would be good to check if the string is empty. If the string is empty it would throw an exception.

接受的答案很好,但是如果您使用它从 android 中的 textView 获取值,最好检查字符串是否为空。如果字符串为空,则会抛出异常。

private String capitizeString(String name){
    String captilizedString="";
    if(!name.trim().equals("")){
       captilizedString = name.substring(0,1).toUpperCase() + name.substring(1);
    }
    return captilizedString;
}

回答by Shrini Jaiswal

Please create a custom TextView and use it :

请创建一个自定义 TextView 并使用它:

public class CustomTextView extends TextView {

    public CapitalizedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (text.length() > 0) {
            text = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
        }
        super.setText(text, type);
    }
}

回答by Hiren Patel

For me none of working:

对我来说没有工作:

Function:

功能:

private String getCapsSentences(String tagName) {
    String[] splits = tagName.toLowerCase().split(" ");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < splits.length; i++) {
        String eachWord = splits[i];
        if (i > 0 && eachWord.length() > 0) {
            sb.append(" ");
        }
        String cap = eachWord.substring(0, 1).toUpperCase() 
                + eachWord.substring(1);
        sb.append(cap);
    }
    return sb.toString();
}

Result:

结果:

I/PbrainO/PBrain

I/P brainO/P大脑

I/PBrain and HealthO/PBrain And Health

输入/输出输入/Brain and Health输出Brain And Health

I/Pbrain And healthto O/PBrain And Health

I/Pbrain And healthO/PBrain And Health

I/Pbrain's Healthto O/PBrain's Health

I/Pbrain's HealthO/PBrain's Health

I/Pbrain's Health and legto O/PBrain's Health And Leg

I/Pbrain's Health and legO/PBrain's Health And Leg

Hope this would help you.

希望这会帮助你。

回答by VinceMedi

For Kotlin, if you want to be sure that the format is "Aaaaaaaaa" you can use :

对于 Kotlin,如果您想确保格式为“Aaaaaaaaa”,您可以使用:

myString.toLowerCase(Locale.getDefault()).capitalize()