我发现R.string
非常棒,可以将硬编码的字符串保留在我的代码中,并且我希望继续在实用程序类中使用它,该实用程序类与我的应用程序中的模型一起生成输出。例如,在这种情况下,我正在从活动之外的模型生成电子邮件。
是否有可能在getString
或Context
之外使用Activity
?我想我可以通过当前的活动,但似乎没必要。如果我错了请纠正我!
编辑:我们可以在不使用Context
的情况下访问资源吗?
您可以使用:
Resources.getSystem().getString(android.R.string.somecommonstuff)
...在您的应用程序中的任何地方,甚至在静态常量声明中。不幸的是,它仅支持系统资源。
对于当地资源,请使用this solution。这不是微不足道的,但它确实有效。
这就是我所做的,在您的MainActivity中,为上下文创建一个静态变量,如下所示:
public static Context mContext;
并在onCreate()初始化mContext中;
mContext = this;
然后,在您要访问上下文的文件中,例如,
private Context context = MainActivity.mContext;
现在,您可以按以下方式获取字符串资源,
String myString = context.getResources().getString(R.string.resource_id);
我用getContext().getApplicationContext().getString(R.string.nameOfString);
它对我有用。
不幸的是,你可以访问任何字符串资源的唯一方法是使用Context
(即Activity
或Service
)。在这种情况下我通常做的是简单地要求调用者传入上下文。
在MyApplication
,扩展Application
:
public static Resources resources;
在MyApplication
的onCreate
:
resources = getResources();
现在,您可以在应用程序的任何位置使用此字段。
BTW,符号未找到错误的原因之一可能是您的IDE导入了android.R;而不是你的一个。只需更改import android.R;导入your.namespace.R;
所以在不同的类中可以看到2个基本的东西:
//make sure you are importing the right R class
import your.namespace.R;
//don't forget about the context
public void some_method(Context context) {
context.getString(R.string.YOUR_STRING);
}
App.getRes().getString(R.string.some_id)
这将适用于app中的任何地方。 (Util类,Dialog,Fragment或您应用中的任何类)
(1)创建或编辑(如果已经存在)你的Application
类。
import android.app.Application;
import android.content.res.Resources;
public class App extends Application {
private static App mInstance;
private static Resources res;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
res = getResources();
}
public static App getInstance() {
return mInstance;
}
public static Resources getResourses() {
return res;
}
}
(2)在manifest.xml
<application
标签中添加名称字段。
<application
android:name=".App"
...
>
...
</application>
现在你很高兴。在app中的任何地方使用App.getRes().getString(R.string.some_id)
。
如果您有一个在活动中使用的类,并且您想要访问该类中的ressource,我建议您在类中将上下文定义为私有变量,并在构造函数中初始化它:
public class MyClass (){
private Context context;
public MyClass(Context context){
this.context=context;
}
public testResource(){
String s=context.getString(R.string.testString).toString();
}
}
在您的活动中立即上课:
MyClass m=new MyClass(this);
这可以让你从任何地方访问applicationContext
,让你在任何可以使用它的地方获得applicationContext
; Toast
,getString()
,sharedPreferences
等
单身人士:
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
子类中初始化Singleton:
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"
>
如果你在这里看到任何错误,请告诉我,谢谢。 :)
Khemraj回应的最佳方法:
App类
class App : Application() {
companion object {
lateinit var instance: Application
lateinit var resourses: Resources
}
// MARK: - Lifecycle
override fun onCreate() {
super.onCreate()
instance = this
resourses = resources
}
}
清单中的宣言
<application
android:name=".App"
...>
</application>
常数类
class Localizations {
companion object {
val info = App.resourses.getString(R.string.info)
}
}
运用
textView.text = Localizations.info
没有上下文和活动,最好使用这样的东西:
Resources.getSystem().getString(R.string.my_text)