关于网友提出的“Andoid如何获取string字符串,在一个类中没有context”问题疑问,本网通过在网上对“Andoid如何获取string字符串,在一个类中没有context”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:Andoid如何获取string字符串,在一个类中没有context
描述:我们都知道,Android中在一个Activity或是service中可以用this.getString(R.string,test);来获取字符串,但如果这样呢?
publicclass myTestClass{ public static
final String MYTEST = "this is a
test"; //如果这个字符串是在string.xml中,该如何获取该值
//this.getString()无法获取
}
解决方案1:从构造函数中传入Context对象或者Resource对象即可。
解决方案2:可以传个Resources进去,或者用个工具类继承Application,弄一个全局的Context(单例),并在XML中的Application中声明,类似与这样 android:name="test"
解决方案3:自己实现/重载 getString() 方法
解决方案4:你得传入一个上下文context啊
解决方案5:如果你想要系统级别的,可以这样.
Resources.getSystem().getString(android.R.string.xxxxxx);
android.R.string.xxxxxx 为系统中已经定义的资源.
以上介绍了“Andoid如何获取string字符串,在一个类中没有context”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/wd/676720.html