2011-03-09 83 views
1

我有一個按鈕,當它被點擊時會亂放一個單詞並將該單詞放入一個textview中。textview只顯示大寫字母

我希望textview只顯示大寫字母。

我該怎麼做?

謝謝。

回答

-2

如果你的意思是你想TextView的大寫被放入它的一切,那麼你可以設置TextView的利用的XML屬性。

如果你的意思,你只需要大寫字母露面前:「您好」

- > 「HT」

然後,你需要的Java代碼一點點(從內存中,未經測試):

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; 
} 

然後,您可以設置TextView的文本:

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

謝謝,我試過這個,但是當我將它添加到像這樣的jumble.setText((removeLowercase(wordJ.toString())))這樣的混亂的單詞函數中時,這似乎不起作用;使編輯文本更容易只有大寫字母? – Tommy 2011-03-09 02:38:12

+1

不用擔心,我設法通過使用android:inputType =「textCapCharacters」來完成此操作。但是你的代碼有助於從混亂的詞中刪除[]。謝謝:) – Tommy 2011-03-09 02:54:14

2

這將文本轉換爲大寫

text.toUpperCase() 
+0

謝謝,但沒有爲ArrayList 定義。目前它被設置爲jumble.setText(wordJ.toString()); 。 – Tommy 2011-03-09 02:07:11

8

使用Android:textAllCaps:真的,那會像一個魅力

<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" /> 

在Java代碼中使用

text.toUpperCase()