在Toast中使用字符串资源

Bar*_*rry 10 java android toast charsequence

我的代码是:

public static void ToastMemoryShort (Context context) {
    CharSequence text = getString(R.string.toast_memoryshort); //error here
    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    return;
    }
Run Code Online (Sandbox Code Playgroud)

但我得到"无法在Eclipse中对类型Context"中的非静态方法getString(int)进行静态引用.我正在准备本地化我的应用程序(将所有硬编码字符串转换为资源),所以我在哪里:

getString(R.string.toast_memoryshort)
Run Code Online (Sandbox Code Playgroud)

我之前有一个硬编码的字符串,很好.

我不确定这里发生了什么(Java noob).请问有人可以开导我吗?

非常感谢

巴兹

Ras*_*sel 22

改成

 public static void ToastMemoryShort (Context context) {

        Toast.makeText(context, context.getString(R.string.toast_memoryshort), Toast.LENGTH_LONG).show();
        return;
        }
Run Code Online (Sandbox Code Playgroud)


Ste*_*ger 5

只需使用它:

makeText(Context context, int resId, int duration) 制作一个标准的 toast,它只包含一个带有来自资源的文本的文本视图。

来自http://developer.android.com/reference/android/widget/Toast.html