enz*_*m83 2 android android-resources
我创建了一个使用TTS引擎向用户发送反馈的应用程序.为了提高性能,我使用了synthesizeToFile和addSpeech方法,但是要合成的文本字符串在strings.xml文件中,所以我必须为TTS引擎说出的每个字符串调用这些方法.
由于TTS引擎仅使用其名称开头的字符串tts_,是可以很容易地遍历打头的所有字符串tts_的内部strings.xml文件?
Log*_*kup 10
您可以通过反射获取strings.xml中的所有字符串,并仅过滤掉您需要的字符串,如下所示:
for (Field field : R.string.class.getDeclaredFields())
{
if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class))
{
try
{
if (field.getName().startsWith("tts_"))
{
int id = field.getInt(null);
// do something here...
}
} catch (IllegalArgumentException e)
{
// ignore
} catch (IllegalAccessException e)
{
// ignore
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将所有资源名称(在定义时)指定为"prefix"+(1..n). 而在代码使用中,
int resid=<constant>;
for(i=1;resid!=0;i++){
resid = this.getResources().getIdentifier("prefix"+i, "strings", this.getPackageName());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |