How to easily iterate over all strings within the “strings.xml” resource file?

后端 未结 4 1521
刺人心
刺人心 2021-01-22 08:36

I created an application that uses the TTS engine to send feedback to the user. With the aim to improve the performance, I used the synthesizeToFile and addSp

4条回答
  •  粉色の甜心
    2021-01-22 08:48

    You can get all the strings in strings.xml via reflection, and filter out only the ones you need, like so:

    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
        }
      }
    }
    

提交回复
热议问题