简体   繁体   English

Android 在 String.xml 中设置字符串值

[英]Android set String value in String.xml

can anybody help me??有谁能够帮我?? I want to add String value through coding in string.xml我想通过在 string.xml 中编码来添加字符串值
I am doing this.我正在这样做。

  String name = getResources().getString(R.string.name);
  if(name.lenght() < 1 ){
      // getResources().setString(R.string.name);??????????????????????

  }

My string.xml is我的 string.xml 是

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <string name="name"></string>
 </resources>

does anybody know how i can add value of name in string.xml though coding.有人知道我如何通过编码在 string.xml 中添加名称值。 Thank you!!谢谢!!

The resources are pretty much set in stone, so you can't modify them at runtime.资源几乎是一成不变的,因此您无法在运行时修改它们。 If you need to store some new strings, use SharedPreferences or SQLite .如果您需要存储一些新字符串,请使用SharedPreferencesSQLite

I don't think you want to do that.我不认为你想那样做。 If you are trying to store a value in a persistent way, take a look at SharedPrefences .如果您尝试以持久的方式存储值,请查看SharedPrefences Google has a good introduction to it here .谷歌在这里有一个很好的介绍。

It is not possible to modify the resources of an APK during runtime.无法在运行时修改 APK 的资源。

You can't edit those resources directly.您不能直接编辑这些资源。 You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.

You cant edit a resource or add a resource once the code is compiled.编译代码后,您将无法编辑资源或添加资源。 I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory.我不知道 setResource 到底是做什么的,但是一旦你的程序被编译,android 会构建 gen 文件,为这些变量指定一定的空间,一旦写入更改变量会导致 memory 的溢出或越界错误。 If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.如果您想要持久值,请尝试使用存储在应用程序目录中的 SharedPrefs、SQL 甚至您自己的 XML,您可以将其设置为只能由您的应用程序读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM