Android 为 TextView 设置大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12159342/
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 09:55:56  来源:igfitidea点击:

Set upper case for TextView

androidandroid-layout

提问by Tim

I have TextView. I would like to show text in upper case mode. Is there attributed for upper case? Just I have text in strings.xml and I need use the line in several place as lower case and upper case.

我有文本视图。我想以大写模式显示文本。是否有大写的归属?只是我在 strings.xml 中有文本,我需要在几个地方使用该行作为小写和大写。

回答by Ben Hutchison

In the layout XML, you can set the android:textAllCapsattribute on your TextView:

在布局 XML 中,您可以android:textAllCaps在您的TextView:

<TextView android:textAllCaps="true"></TextView>

回答by Adil Soomro

Use Stringclass method while setting the text to TextViewas

使用String类的方法,而文本设置TextView

tv.setText(strings.toUpperCase());

Have a look at thispost for detail discussion.

看看这篇文章的详细讨论。

EDIT:This was answered way back before API 14. As other answer mentioned here, textView.isAllCaps = trueand its java methods were introduced from TextView. Please use those.

编辑:这是在 API 14 之前回答的。正如这里提到的其他答案,textView.isAllCaps = true它的 java 方法是从TextView. 请使用那些。

回答by Rajesh

use this for programatically like textView.setAllCaps(false)for as it is and textView.setAllCaps(true)for Uppercase android:textAllCaps="true"for XML Layout

以编程方式使用它就像textView.setAllCaps(false)for as it is and textView.setAllCaps(true)for Uppercase android:textAllCaps="true"for XML Layout

回答by yousef

use it

用它

<TextView android:textAllCaps="true"></TextView>

回答by calsign

For an XML approach, you don't want to use android:capitalizebecause this is intended for use during text input. Instead, use textAllCaps. If your strings are declared as lower case, then it is quite simple to toggle between upper and lower case on a per-TextView basis.

对于 XML 方法,您不想使用,android:capitalize因为它旨在在文本输入期间使用。相反,使用textAllCaps。如果您的字符串被声明为小写,那么在每个 TextView 的基础上在大写和小写之间切换是非常简单的。

回答by Metu

Kotlin:

科特林:

textView.isAllCaps = true

回答by slezadav

I believe there is no such attribute, but you can use

我相信没有这样的属性,但你可以使用

textView.setText(text.toUpperCase());

also found this, never tested by myself though

也发现了这一点,虽然我自己从未测试过

android:capitalize="characters"

回答by Chirag

Try this.

尝试这个。

Upper Case

大写

textView.setText(getResources().getString(R.string.app_name).toUpperCase());

Lower Case

小写

textView.setText(getResources().getString(R.string.app_name).toLowerCase());

回答by Sameer

You could create a custom view derived from TextView and override the setText method to capitalize.

您可以创建一个从 TextView 派生的自定义视图并覆盖 setText 方法以进行大写。

回答by Gevaria Purva

Set android:textAllCaps="true" in layout file.

在布局文件中设置 android:textAllCaps="true"。