在对小部件执行其他操作(如setText
)之前,我希望从xml
文件中读取字符串,那么如果没有activity对象来调用getResources()
,我如何做到这一点呢?
发布于 2019-10-30 22:36:24
我的Kotlin解决方案是使用静态应用程序上下文:
class App : Application() {
companion object {
lateinit var instance: App private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
还有Strings类,我在任何地方都会用到:
object Strings {
fun get(@StringRes stringRes: Int, vararg formatArgs: Any = emptyArray()): String {
return App.instance.getString(stringRes, *formatArgs)
}
}
因此,您可以以一种干净的方式获取资源字符串
Strings.get(R.string.some_string)
Strings.get(R.string.some_string_with_arguments, "Some argument")
请不要删除此答案,让我保留一个。
发布于 2017-02-12 17:07:18
还有另一种可能性。我从如下资源加载OpenGl着色器:
static private String vertexShaderCode;
static private String fragmentShaderCode;
static {
vertexShaderCode = readResourceAsString("/res/raw/vertex_shader.glsl");
fragmentShaderCode = readResourceAsString("/res/raw/fragment_shader.glsl");
}
private static String readResourceAsString(String path) {
Exception innerException;
Class<? extends FloorPlanRenderer> aClass = FloorPlanRenderer.class;
InputStream inputStream = aClass.getResourceAsStream(path);
byte[] bytes;
try {
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
innerException = e;
}
throw new RuntimeException("Cannot load shader code from resources.", innerException);
}
如您所见,您可以访问path /res/...
中的任何资源,并将aClass
更改为您的类。这也是我在测试中加载资源的方式(androidTests)
发布于 2015-09-25 19:45:03
单例:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
在您的Application
子类中初始化单例:
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
如果我没记错的话,这给了你一个到处applicationContext的钩子,用ApplicationContextSingleton.getInstance.getApplicationContext();
调用它你不需要在任何时候清除它,因为当应用程序关闭时,无论如何这都会伴随着它。
记住要更新AndroidManifest.xml
以使用这个Application
子类:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
现在你应该可以在任何地方使用ApplicationContextSingleton.getInstance().getApplicationContext().getResources()了,应用程序子类不能使用的地方也很少。
如果你发现这里有什么问题,请告诉我,谢谢。:)
https://stackoverflow.com/questions/4391720
复制相似问题