开发者

textview only display capital letters

I h开发者_JAVA技巧ave a button which when clicked jumbles a word and puts the word into a textview.

I want the textview to only display capital letters.

How can I do this?

Thanks.


Use android:textAllCaps:true it will work like a charm

 <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:text="@string/app_name"
    android:textAllCaps="true"
    android:layout_gravity="center_vertical"
   android:textStyle="bold"
    android:typeface="sans" />

In Java code use

   text.toUpperCase() 


This will convert the text to all uppercase

text.toUpperCase() 


If you mean you want the TextView to capitalize everything that is put into it, then you can set the capitalize XML attribute on the TextView.

If you mean you only want capital letters to show up ex:

"Hi There!" --> "HT"

Then you need a little bit of Java code (from memory, untested):

private static String removeLowercase(String input)
{
   if(input == null)
     return null;

   String retVal = "";

   for(int i=0; i < input.length(); i++)
   {
        char c = input.charAt(i);

        if(Character.isUpperCase(c))
            retVal += c;
   }

   return retVal;
}

You can then set the text in the TextView:

myTV.setText(removeLowercase("SomeInput"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜