getString在Context或Activity之外

Sap*_*Sun 236 java string android android-context android-resources

我发现R.string将硬编码字符串保留在我的代码中非常棒,我想继续在一个实用程序类中使用它,该实用程序类与我的应用程序中的模型一起生成输出.例如,在这种情况下,我正在从活动之外的模型生成电子邮件.

是否可以在getString外面使用ContextActivity?我想我可以通过当前的活动,但似乎没必要.如果我错了请纠正我!

编辑:我们可以使用访问资源Context吗?

Gan*_*nus 405

是的,我们可以在不使用`Context`的情况下访问资源

您可以使用:

Resources.getSystem().getString(android.R.string.somecommonstuff)
Run Code Online (Sandbox Code Playgroud)

...在您的应用程序中的任何地方,甚至在静态常量声明中.不幸的是,它支持系统资源.

对于本地资源使用该解决方案.这不是微不足道的,但它确实有效.

  • 系统资源是什么意思?strings.xml是否是系统资源?对我来说它不起作用,说无法找到资源. (13认同)
  • 我收到此错误“android.content.res.Resources$NotFoundException:字符串资源 ID #0x7f0f0061” (11认同)
  • 系统资源属于设备上的Android.strings.xml仅属于您的应用程序.寻找http://stackoverflow.com/a/4391811/715269解决方案 (6认同)
  • 这是访问字符串时这些Factory类的优雅解决方案.我不喜欢到处传递Context.对于我们真正想要全局存储字符串的情况,这只是不必要的混乱. (3认同)

Eri*_*ass 107

不幸的是,你可以访问任何字符串资源的唯一方法是使用Context(即一个ActivityService).在这种情况下我通常做的是简单地要求调用者传入上下文.

  • 答案是假的.看下一个.:-) (11认同)
  • 谢谢你的提示!我只是试过这个,但由于某种原因,当我尝试时遇到编译错误:`ctx.getString(ctx.R.string.blah);` (4认同)
  • 你不需要`ctx.R.string.blah`,只需使用`R.string.blah` (2认同)
  • 我不确定`符号未找到错误'来自何处,但请确保在课程顶部导入`R`. (2认同)

kon*_*mik 33

MyApplication,扩展Application:

public static Resources resources;
Run Code Online (Sandbox Code Playgroud)

MyApplication's onCreate:

resources = getResources();
Run Code Online (Sandbox Code Playgroud)

现在,您可以在应用程序的任何位置使用此字段.


Jan*_*icz 22

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);
}
Run Code Online (Sandbox Code Playgroud)


Khe*_*raj 15

Unique Approach

App.getRes().getString(R.string.some_id)

This will work everywhere in app. (Util class, Dialog, Fragment or any class in your app)

(1) Create or Edit (if already exist) your Application class.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

(2) Add name field to your manifest.xml <application tag.

<application
        android:name=".App"
        ...
        >
        ...
    </application>
Run Code Online (Sandbox Code Playgroud)

Now you are good to go. Use App.getRes().getString(R.string.some_id) anywhere in app.


Mic*_*sen 6

Khemraj 的回应中的最佳方法:

应用类

class App : Application() {

    companion object {
        lateinit var instance: Application
        lateinit var resourses: Resources
    }


    // MARK: - Lifecycle

    override fun onCreate() {
        super.onCreate()
        instance = this
        resourses = resources
    }

}
Run Code Online (Sandbox Code Playgroud)

清单中的声明

<application
        android:name=".App"
        ...>
</application>     
Run Code Online (Sandbox Code Playgroud)

常量类

class Localizations {

    companion object {
        val info = App.resourses.getString(R.string.info)
    }

}
Run Code Online (Sandbox Code Playgroud)

使用

textView.text = Localizations.info
Run Code Online (Sandbox Code Playgroud)