2012年7月27日金曜日
java.text.MessageFormat
MessageFormatは何をするか
アプリケーションでは、エンドユーザに表示するメッセージがほとんどは定められたフォーマットに沿って出力されています。
たとえば、
ユーザIDを入力してください。
金額を入力してください。
などのメッセージでは、【を入力してください。】は同じです。
MessageFormatは、このようなメッセージを統一したパターンで簡単にフォーマットすることができます。
MessageFormatの使用方法(手順)
1)メッセージパターンを定義します。
例:{0}を入力してください。
フォーマットしたい要素は{}で囲まれます。
2)置き換えデータを配列に設定し、MessageFormat.formatメソッドでフォーマットします。
例:String message = MessageFormat.format("{0}を入力してください。", new String[]{"ユーザID"});
戻り値は置き換えた後の文字列になります。
java.text.MessageFormat構造
java.text.Format
�java.text.MessageFormat
■コンストラクタ
◇ MessageFormat(String pattern)
◇ MessageFormat(String pattern, Locale locale)
パラメータ pattern:メッセージフォーマット・パターン
パラメータ locale:ロケール
■MessageFormat主たるメソッド
◇ StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
◇ StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)
◇ static String format(String pattern, Object... arguments)
※JDK1.5以前のバージョンでは、static String format(String pattern, Object []arguments)になります。
指定された文字列パターンをフォーマット要素に置き換えフォーマットします。
◇ Object[] parse(String source)
文字列を解析してパターンとマッチしたオブジェクトの配列を返します。
■フォーマット要素の定義
フォーマット要素は{}で囲まれます。
{ ArgumentIndex }
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
◇ ArgumentIndex:0,1,2...などのインデックスを表す数字。置き換えデータの配列のインデックス
◇ FormatType:number(数字)、date(日付)、time(日時)、choice(ある範囲の数値)などの文字
◇ FormatStyle:
short
medium
long
full
integer
currency
percent
SubformatPattern(サブフォーマットパターン)
◇ 定義例:
{0}
{0,number,integer}
{1,number,#.##}
サンプル
TestMessageFormat.java
view plaincopy to clipboardprint?
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class TestMessageFormat {
/**
* MessageFormatのよくある使い方
*/
public static void main(String[] args) {
System.out.println("********* 1 ***********");
String message = MessageFormat.format("{0}を入力してください。", new Object[]{"ユーザID"});
System.out.println(message);
System.out.println("********* 2 ***********");
String pattern2 = "a={0,number,#.##} , b={1,number,integer} , c={2,date}";
Object []arguments = {
new Float(123.4567),
new Integer(345),
new Date()
};
String text2 = MessageFormat.format(pattern2, arguments);
System.out.println(pattern2);
System.out.println(text2);
System.out.println("********* 3 ***********");
String pattern3 = "a={0} , b={1,number,percent} , c={2,time}";
System.out.println(pattern3);
Object []arguments3 = {
"Hello MessageFormat!",
new Float(0.778),
Calendar.getInstance().getTime()
};
MessageFormat messageFormat = new MessageFormat(pattern3);
String text3 = messageFormat.format(arguments3);
System.out.println(text3);
System.out.println("********* 4 ***********");
String pattern4 = "a={0} , b={1} , c={2}";
String message4 = "a=100 , b=あいうえお , c=2009/03/05";
System.out.println(pattern4);
System.out.println(message4);
MessageFormat messageFormat4 = new MessageFormat(pattern4);
try {
Object []objs = messageFormat4.parse(message4);
System.out.println(objs[0]);
System.out.println(objs[1]);
System.out.println(objs[2]);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
実行結果
********* 1 ***********
ユーザIDを入力してください。
********* 2 ***********
a={0,number,#.##} , b={1,number,integer} , c={2,date}
a=123.46 , b=345 , c=2009/03/05
********* 3 ***********
a={0} , b={1,number,percent} , c={2,time}
a=Hello MessageFormat! , b=78% , c=14:38:25
********* 4 ***********
a={0} , b={1} , c={2}
a=100 , b=あいうえお , c=2009/03/05
100
あいうえお
2009/03/05
2012年7月26日木曜日
difference between getMainLooper() and Looper.myLooper()
| I'm now trying to resurrect one project. There was an exception on getMainLooper()... I thought that may be there's a problem with MainLooper initialization and added Looper.prepareMainLoop() before that. Exception telling me that there's already a looper for that object was thrown... Then I tried to replace getMainLooper() with Looper.myLooper() and it worked... But I didn't understand why=) In fact I don't get the difference between this two things. I think that on the place where getMainLooper() was used in my project it's the best place for the true main looper of the application but I got what I got.. Please explain. Thank you for your attention |
The difference is that Looper.preapreMainLooper() prepares looper in main UI thread. Android applications normally do not call this function. As main thread has its looper prepared long before first activity, service, provider or broadcast receiver is started.
But Looper.prepareLooper() prepares Looper in current thread. After this function is called, thread can call Looper.loop() to start processing messages with Handlers.
So, in your case you had two threads - X and Y. The X thread is the main UI thread that has its looper already prepared by Android. When you are in Y thread and you're callingLooper.prepareMainLooper() you're trying to prepare looper in X thread (main thread). This fails because X's looper is laredy prepared. But when you call Looper.prepareLooper() in Y thread, you're actually preparing looper in Y thread and therefore ready to call Looper.loop()