java android:大写不工作

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

android:capitalize not working

javaandroidxml

提问by emachine

I have a TextView. I'm trying to capitalize the first letter in every word.

我有一个文本视图。我试图将每个单词的第一个字母大写。

Here's the TextView:

这是文本视图:

 <TextView 
            android:text="TextView" 
            android:id="@+id/textView1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_alignParentRight="true" 
            android:textSize="30dip" 
            android:textStyle="bold" 
            android:layout_marginRight="5dip" 
            android:ellipsize="end"
            android:capitalize="words">
        </TextView>

Here's how I'm adding the text:

这是我添加文本的方式:

TextView titleView = (TextView) findViewById(R.id.textView1);
    titleView.setText( section.replace("_", " ") );

Can I not add text dynamically and expect it to capitalize the words? Is another trait interfering with android:capitalize? Is android:capitalizebroken?

我可以不动态添加文本并期望它大写单词吗?是否有其他特征干扰了android:capitalize?被android:capitalize打破?

Thanks for your replies.

感谢您的回复。

回答by eldarerathis

capitalizeis basically just a KeyListenerthat you can set in XML, so it only applies to text input by the user. As the documentation states(emphasis mine):

capitalize基本上只是一个KeyListener你可以在XML中设置的,所以它只适用于用户输入的文本。正如文档所述(强调我的):

If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.

如果设置,则指定此 TextView 具有文本输入法并应自动将用户键入的内容大写。

There is a related question on how to capitalize the first letter of every word in Javawhich has some helpful answers.

有一个关于如何将 Java 中每个单词的首字母大写的相关问题,其中有一些有用的答案。

回答by Nir Pear

If you're targeting API Level 14 and above, you should use

如果您的目标是 API 级别 14 及以上,则应使用

android:textAllCaps="true"

机器人:textAllCaps =“真”

Otherwise, you'll have to implement this behavior yourself.

否则,您必须自己实现此行为。

回答by ban-geoengineering

android:capitalizeis now deprecated.

android:capitalize现在已弃用。

Instead of using android:capitalize="words", you should consider using android:inputType="textCapWords".

android:capitalize="words"您应该考虑使用 ,而不是使用android:inputType="textCapWords"

Depending on your needs, you can also use multiple values, such as android:inputType="textCapWords|textPersonName".

根据您的需要,您还可以使用多个值,例如android:inputType="textCapWords|textPersonName".

回答by Ahmed

This is a late answer but I think might help someone...

这是一个迟到的答案,但我认为可能会帮助某人......

If you are comfortable with capitilizing the dynamic text in the java code then you can use:

如果您对在 Java 代码中使用大写动态文本感到满意,那么您可以使用:

textView.setText(text.toUpperCase()); 

回答by user3259330

@Shine's answer is correct I don't know why it was down voted. android:capitalize was deprecated in API 3. Unfortunately the TextView docs fail to indicate this, the proof is burried in R.attr:

@Shine 的回答是正确的,我不知道为什么它被否决了。android:capitalize 在 API 3 中已被弃用。不幸的是 TextView 文档未能指出这一点,证据被埋在 R.attr 中:

R.attr

属性

Android studio also fails to inform you that this attribute is deprecated. Another 30 mins I'll never get back, thanks google!

Android studio 也无法通知您此属性已弃用。再过 30 分钟我再也回不去了,谢谢谷歌!

回答by Shine

android:capitalize is deprecated on ICS, so I guess it should be better to do it in code (i.e. with String.toUpperCase())

android:capitalize 在 ICS 上已被弃用,所以我想最好在代码中进行(即使用 String.toUpperCase()

Another try could be

另一种尝试可能是

 android:inputType="textCapWords"

but I guess it would require an EditText to work.

但我想它需要一个 EditText 才能工作。

I don't know if this is the reason for your code, what version are you targeting?

我不知道这是否是您代码的原因,您的目标版本是什么