从类中调用 getString(R.strings....) ?

发布于 2024-12-05 05:33:47 字数 110 浏览 2 评论 0原文

有没有办法使用单独的类中的 getString 方法?

我的字符串 xml 中存储了一个字符串,我想在对象中使用该字符串...但该方法甚至在对象中不可用...

有什么提示吗?

Is there a way to use the getString method from a seperate class?

I have a string stored in my strings xml, I'd like to use that string in an object... but the method isn't even available in the object...

any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流年已逝 2024-12-12 05:33:47

getString()Context 的方法 类 1。如果您在单独的类(不扩展 Context)中需要它,通常最好将其作为单独的参数提供给需要它的方法。

示例:

public void logString(Context c, int stringId) {
    Log.d("TAG", c.getString(stringId));
}

有一件事很重要:永远不要将上下文存储在单独的类中。
提供一个论据。否则,如果存储上下文的对象比上下文最初所属的对象(例如活动)的寿命长,您将泄漏内存并破坏整个 android 生命周期。

¹ getString() 也可以在 Resources 类 - 您可以通过 Context.getResources() 获取

getString() is a method of the Context class¹. If you need it inside a seperate class (that does not extend Context), it's usually best to provide it as a seperate argument to the method that needs it.

Example:

public void logString(Context c, int stringId) {
    Log.d("TAG", c.getString(stringId));
}

One thing is important: Never store the context inside the separate class.
Provide an argument. Otherwise you will leak memory and disrupt the whole android lifecycle if the object that stores the context lives longer than the object where the context originally belongs to (e.g. an activity).

¹ getString() can also be used from the Resources class - which you can get via Context.getResources()

秋千易 2024-12-12 05:33:47

这里的解决方案是确保您的对象具有对应用程序上下文的引用

Class Swag{
    private Context ctx;
    public Swag(Context ctx){
       this.ctx = ctx;
    }
    public void doSomething(){
        String something = ctx.getResources().getString(R.string.somestring);
        ...
    }
    // or like this
    public void makeUpperCase(Context appContext){
        appContext.getResources().getString(R.string.super_string_swag_yolo);
    }
}

,显然您必须在创建对象或调用方法时提供上下文

the solution here is to make sure your object has a reference to the application context

Class Swag{
    private Context ctx;
    public Swag(Context ctx){
       this.ctx = ctx;
    }
    public void doSomething(){
        String something = ctx.getResources().getString(R.string.somestring);
        ...
    }
    // or like this
    public void makeUpperCase(Context appContext){
        appContext.getResources().getString(R.string.super_string_swag_yolo);
    }
}

obviously you'd have to supply the context when creating an object or when caling the method

盗梦空间 2024-12-12 05:33:47

资源文件:values/strings.xml

<resources>
   <string name="app_name">App name</string>
<resources>

java

import android.content.res.Resources;
Resources.getSystem().getString(R.string.app_name);//result : App name

resouce file: values/strings.xml

<resources>
   <string name="app_name">App name</string>
<resources>

java

import android.content.res.Resources;
Resources.getSystem().getString(R.string.app_name);//result : App name
心的憧憬 2024-12-12 05:33:47

编辑:
下面的将不起作用。我在另一个网站上读到了这篇文章并认为它有效,但我只是在我的应用程序中尝试过它并不断收到错误。问题是,它会编译,但你会得到一个运行时异常。

这适用于任何 java 类:

import android.content.res.Resources

Resources.getSystem().getString(R.string.blah);

edit:
The below will NOT work. I read this on another site and assumed it worked, but I just tried it in my app and kept getting an error. Problem is, it will compile but you will get a runtime exception.

This will work from any java class:

import android.content.res.Resources

Resources.getSystem().getString(R.string.blah);
时光清浅 2024-12-12 05:33:47

如果您无法将上下文作为参数传递,请创建另一个类,在其中放置所有静态数据。

示例:

public class StaticData {
    public static String BASE_URL = "https://stackoverflowrocks.com";
}

并通过直接调用从其他类获取该字符串
StaticData.BASE_URL

干净整洁。

if you cannot pass a context as parameter, create another class, where you put all your static data.

example :

public class StaticData {
    public static String BASE_URL = "https://stackoverflowrocks.com";
}

and get that string from your other class by calling directly
StaticData.BASE_URL

nice and clean.

雪化雨蝶 2024-12-12 05:33:47

最佳解决方案在这里:

首先,您应该将 ApplicationContext 注入到您的类中,然后调用
ContextCompat.getContextForLanguage(applicationContext),然后在此上下文上 getString(resId)

文档说这个方法

获取尊重每个应用程序语言环境的上下文

,因此它非常适合较低 (<= 32) API 版本,其中应用程序上下文未在应用程序语言环境更改时配置,并且调用 getString(resId 将会使在默认区域设置中返回的此字符串

非常安全,因为它的生命周期基于进程生命周期,因此不会泄漏。

Best solution is here:

At first, you should inject ApplicationContext to your class and then call
ContextCompat.getContextForLanguage(applicationContext) and then getString(resId) on this context.

The documentation says that this method

Gets the context which respects the per-app locales locale

So it's pretty suitable for lower (<= 32) API version where the app context is not configured on app locale changes and calling getString(resId will make this string returned in your default locale.

Injecting applicationContext is pretty safe because it's lifetime bases on the process lifetime so it can't be leaked.

握住我的手 2024-12-12 05:33:47

这有效,但仅适用于系统资源:

import android.content.res.Resources
Resources.getSystem().getString(R.string.blah);

参考:https://stackoverflow.com/a/40917607/8994882

This works, but for SYSTEM resources only:

import android.content.res.Resources
Resources.getSystem().getString(R.string.blah);

Reference: https://stackoverflow.com/a/40917607/8994882

森林迷了鹿 2024-12-12 05:33:47

在您的 java 文件中尝试以下操作:

String myString = getResources().getString(R.string.MY_STRING)

现在使用此字符串对象。

Try this in your java file:

String myString = getResources().getString(R.string.MY_STRING)

Now use this string object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文