Android:以编程方式更改字符串资源

rya*_*dlf 19 string resources android

有没有办法做到这一点?例如,我的偏好设置可以在公制和标准计量单位之间进行转换,当用户更改此设置时,我想更新几个字符串以更改我的应用程序中的某些标签.

小智 24

您可以在TextView或类似的任何内容上更新标签.

  • 使用获取更新的字符串 update_str = Context.getResources().getString (R.string.update_identifier)
  • 使用更新标签 ((TextView)findViewById (R.id.textview)).setText (updated_str)


Dar*_*pan 9

您可以使用以下说明从strings.xml文件中获取已保存的标签,并在某些"if语句"中相应地显示它们,这可能会检查您的

pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) .getString("conversion_mode", "");

String string = getString(R.string.update_identifier);
Run Code Online (Sandbox Code Playgroud)

要么

getResources().getString (R.string.update_identifier)
Run Code Online (Sandbox Code Playgroud)

会做得最好

getString(int)或者getText(int)可以用于检索字符串.请注意,getText(int)将保留应用于字符串的任何富文本样式.

找到参考