Java 根据Android上使用的语言,如何检查消息的正确性?
我通过Robotium检查屏幕上的文本方法:Java 根据Android上使用的语言,如何检查消息的正确性?,java,android,robotium,Java,Android,Robotium,我通过Robotium检查屏幕上的文本方法: assertTrue(solo.waitForText("Wrong password")); 或 但是,我的应用程序可以使用多种语言, 同时使用Android中指定的语言作为使用的语言。应用程序是用Java编写的。根据Android上使用的语言,如何检查消息的正确性 您可以从测试项目访问原始项目的字符串资源,因此可以访问原始字符串的ID。如果您希望支持更多语言,而不是使用本地化字符串资源,请访问以下链接: 如果希望在不更改当前配置的情况下获取
assertTrue(solo.waitForText("Wrong password"));
或
但是,我的应用程序可以使用多种语言, 同时使用Android中指定的语言作为使用的语言。应用程序是用Java编写的。根据Android上使用的语言,如何检查消息的正确性 您可以从测试项目访问原始项目的字符串资源,因此可以访问原始字符串的ID。如果您希望支持更多语言,而不是使用本地化字符串资源,请访问以下链接:
如果希望在不更改当前配置的情况下获取本地化字符串(API>=17): 其中,
locale
是您要测试的语言环境
如果您的目标是较低的API级别:
Configuration conf = context.getResources().getConfiguration();
conf.locale = new Locale("de");
// Then update resources based on this Configuration.
context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
// Afterwards, resources will retrieve localised strings instead of default locale
String deLocaleString = context.getResources().getString(R.string.my_string);
您不能使用
context.getResources().getString(R.string.ErrorPass)
?
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(locale);
return context.createConfigurationContext(config).getText(resId);
Configuration conf = context.getResources().getConfiguration();
conf.locale = new Locale("de");
// Then update resources based on this Configuration.
context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
// Afterwards, resources will retrieve localised strings instead of default locale
String deLocaleString = context.getResources().getString(R.string.my_string);