我发现R.string非常棒,可以将硬编码的字符串排除在代码之外,我希望在与应用程序中的模型一起工作的实用程序类中继续使用它来生成输出。例如,在本例中,我从活动外部的模型生成了一封电子邮件。

是否可以在上下文或活动之外使用getString ?我想我可以通过目前的活动,但似乎没有必要。如果我说错了,请指正!

编辑:我们可以在不使用上下文的情况下访问资源吗?


当前回答

如果你想在上下文或活动之外使用getString,你应该在构造函数或方法参数中有上下文,这样你就可以访问getString()方法。 特别是在Fragment中,你应该确保getActivity()或getContext()没有提供空值。 要避免在Fragment中getActivity()或getContext()为空,请尝试以下方法: 声明一个变量:

Context mContext;

现在重写Fragment的onAttach和onDetach方法:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

@Override
public void onDetach() {
    super.onDetach();
    mContext = null;
}

现在使用mContext无论你在哪里使用getString()方法。 例:

        Toast.makeText(mContext,  mContext.getString(R.string.sample_toast_from_string_file), Toast.LENGTH_SHORT).show();

其他回答

不知何故,我不喜欢存储静态值的俗气解决方案,所以提出了一个更长的,但干净的版本,可以测试以及。

找到了两种可能的方法

通过上下文。Resources作为类的参数,在其中您需要字符串资源。相当简单。如果不能作为参数传递,则使用setter。

e.g.

data class MyModel(val resources: Resources) {
    fun getNameString(): String {
        resources.getString(R.string.someString)
    }
}

使用数据绑定(尽管需要片段/活动)

阅读之前:本版本使用数据绑定

XML-

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="someStringFetchedFromRes"
        type="String" />
</data>

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@{someStringFetchedFromRes}" />
</layout>

活动/片段-

val binding = NameOfYourBinding.inflate(inflater)
binding.someStringFetchedFromRes = resources.getString(R.string.someStringFetchedFromRes)

有时,您需要根据模型中的字段更改文本。所以你也会数据绑定那个模型,因为你的activity/fragment知道这个模型,你可以很好地获取值,然后基于它数据绑定字符串。

# #独特的方法

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

这将在应用程序的任何地方工作(Util类,对话框,片段或 应用程序中的任何类)

(1)创建或编辑(如果已经存在)你的应用程序类。

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 getRes() {
        return res;
    }

}

(2)在manifest.xml <application标签中添加name字段。

<application
        android:name=".App"
        ...
        >
        ...
    </application>

现在可以开始了。在应用程序的任何地方使用app. getres (). getstring (r.r string.some_id)。

以下是基于Khemraj Sharma的回答,这是Kotlin版本,有一个额外的扩展作为奖励:

class App: Application() {

    override fun onCreate() {
        super.onCreate()
        mInstance = this
        res = resources
    }
    companion object{

        private var mInstance: App? = null
        private var res: Resources? = null

        fun getInstance(): App? {
            return mInstance
        }

        fun getRes(): Resources? {
            return res
        }
    }
}

然后我们可以创建一个扩展:

fun Int.resourceToString(): String {
    return App.getRes()?.getString(this) ?: "Not Found"
}

并直接在任何资源id上使用扩展:

var asString = R.string.my_string.resourceToString()

您可以在Kotlin中通过创建一个扩展Application的类来实现这一点,然后使用它的上下文来调用代码中的任何地方的资源

你的App类看起来是这样的

 class App : Application() {
    override fun onCreate() {
        super.onCreate()
        context = this
    }

    companion object {
        var context: Context? = null
            private set
    }
}

在AndroidManifest.xml中声明你的Application类(非常重要)

<application
        android:allowBackup="true"
        android:name=".App" //<--Your declaration Here
        ...>
        <activity
            android:name=".SplashActivity"  android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"/>
    </application>

要访问字符串文件,请使用以下代码

App.context?.resources?.getText(R.string.mystring)

不幸的是,你能访问任何字符串资源的唯一方法是一个上下文(即一个活动或服务)。在这种情况下,我通常做的是简单地要求调用者传递上下文。