2011年10月5日水曜日

Android Localeに対応した曜日や月の表記一覧を取得する

特定の Locale に対応した、曜日や月などの表記の一覧配列を DateFormatSymbols を使って取得できるので、Android で設定言語を変えた場合を取得してみた。

  1. public class DateFormatSymbolSampleActivity extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.main);  
  6.           
  7.         LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  8.           
  9.         DateFormatSymbols symbols = new DateFormatSymbols();  
  10.           
  11.         TableLayout table = (TableLayout) findViewById(R.id.symbols);  
  12.           
  13.         addTableRow(inflater, table, "getAmPmString", Arrays.toString(symbols.getAmPmStrings()));  
  14.         addTableRow(inflater, table, "getEras", Arrays.toString(symbols.getEras()));  
  15.         addTableRow(inflater, table, "getMonths", Arrays.toString(symbols.getMonths()));  
  16.         addTableRow(inflater, table, "getShortMonths", Arrays.toString(symbols.getShortMonths()));  
  17.         addTableRow(inflater, table, "getWeekdays", Arrays.toString(symbols.getWeekdays()));  
  18.     }  
  19.       
  20.     private void addTableRow(LayoutInflater inflater, TableLayout table, String text1, String text2) {  
  21.         TableRow row = (TableRow)inflater.inflate(R.layout.row, table, false);  
  22.         ((TextView)row.findViewById(R.id.text1)).setText(text1);  
  23.         ((TextView)row.findViewById(R.id.text2)).setText(text2);  
  24.         table.addView(row);  
  25.     }  
  26. }  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TableLayout  
  8.         android:id="@+id/symbols"  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         />  
  12. </LinearLayout>  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableRow xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:padding="5dip"  
  6.     >  
  7.     <TextView  
  8.         android:id="@+id/text1"  
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"   
  11.         android:padding="5dip"  
  12.         />  
  13.     <TextView  
  14.         android:id="@+id/text2"  
  15.         android:layout_width="0dip"  
  16.         android:layout_weight="1"   
  17.         android:layout_height="wrap_content"   
  18.         android:padding="5dip"  
  19.         />  
  20. </TableRow>  


英語ロケール


日本語ロケール


ドイツ語ロケール


フランス語ロケール


韓国語ロケール


ちなみに、1.6 だと、曜日名と月名がとれないバグがあるそうです。
Issue 3985 - android - DateFormatSymbols bugs in Japanese locale sdk1.6 - Project Hosting on Google Code

0 件のコメント:

コメントを投稿