我正在开发一个新的应用程序,我需要创建一个带查找的消息接口,使用键来查找值(如ConstantsWithLookup,但能够接收参数)。我正在调查Dictionary类功能,但缺少通过参数自定义消息。
使用ConstantsWithLookup,我可以进行以下操作:
myConstantsWithLookupInterface.getString("key");
得到类似的东西:
Field must be filled with numbers
但我需要这样做:
myMessagesWithLookupInterface.getString("key", "param1", "param2",...);
得到类似的东西:
Field _param1_ must be filled with numbers greater than _param2_
我不知道如何做到这一点。
答案 0 :(得分:1)
//fields in your class
RegEx pattern1 = RegEx.compile("_param1_");
RegEx pattern2 = RegEx.compile("_param2_");
public String getString(String key, String replace1, String replace2){
// your original getString() method
String content = getString(key);
content = pattern1.replace(content,replace1);
content = pattern2.replace(content,replace2);
return content;
}
如果您的数据包含Field _param1_ must be filled with numbers greater than _param2_
,则会将_param1_
替换为字符串replace1
的内容。