android capitalize,android:capitalize not working

没有检索到摘要

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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

Here's the 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">

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:capitalize broken?

Thanks for your replies.

回答1:

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

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

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

回答2:

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

android:textAllCaps="true"

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

回答3:

android:capitalize is now deprecated.

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

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

回答4:

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:

textView.setText(text.toUpperCase());

回答5:

@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:

R.attr

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

回答6:

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

Another try could be

android:inputType="textCapWords"

but I guess it would require an EditText to work.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值