TextView的大写首字母在Android的布局XML文件 [英] Capitalize first letter of TextView in an Android layout xml file

查看:2000
本文介绍了TextView的大写首字母在Android的布局XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的布局XML文件中的TextView的:

I have a TextView in a layout xml file like this:

<TextView
   android:id="@+id/viewId"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/string_id" />

我的字符串指定这样的:

My string is specified like this:

<string name="string_id">text</string>

是否有可能使它显示文本,而不是文没有Java code
(和不改变字符串本身要么)

Is it possible to make it display "Text" instead of "text" without java code?
(and without changing the string itself either)

推荐答案

我用海仑哈蒙的回答来管理让所有的单词大写。

I used Hyrum Hammon's answer to manage to get all words capitalized.

public class CapitalizedTextView extends TextView {

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

    @Override
    public void setText( CharSequence c, BufferType type ) {

        /* Capitalize All Words */
        try {
            c = String.valueOf( c.charAt( 0 ) ).toUpperCase() + c.subSequence( 1, c.length() ).toString().toLowerCase();
            for ( int i = 0; i < c.length(); i++ ) {
                if ( String.valueOf( c.charAt( i ) ).contains( " " ) ) {
                    c = c.subSequence( 0, i + 1 ) + String.valueOf( c.charAt( i + 1 ) ).toUpperCase() + c.subSequence( i + 2, c.length() ).toString().toLowerCase();
                }
            }
        } catch ( Exception e ) {
            // String did not have more than + 2 characters after space.
        }
        super.setText( c, type );
    }

}

这篇关于TextView的大写首字母在Android的布局XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆