在 Toast 中使用字符串资源

发布于 2024-12-03 02:57:11 字数 512 浏览 1 评论 0原文

我的代码是:

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

但我在 Eclipse 中收到“无法从类型 Context 对非静态方法 getString(int) 进行静态引用”。我正在尝试准备本地化我的应用程序(将所有硬编码字符串放入资源中),所以我有:

getString(R.string.toast_memoryshort)

我以前有一个硬编码字符串,这很好。

我不确定这里发生了什么(Java noob)。有人可以启发我吗?

非常感

谢巴兹

My code is:

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

but I'm getting "Cannot make a static reference to the non-static method getString(int) from the type Context" in Eclipse. I'm trying to get ready for localising my app (getting all the hard coded strings into resources), so where I have:

getString(R.string.toast_memoryshort)

I previously had a hard coded string which was fine.

I'm not sure what's going on here (Java noob). Can anyone enlighten me please?

Many thanks

Baz

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

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

发布评论

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

评论(5

自找没趣 2024-12-10 02:57:11

更改为

 public static void ToastMemoryShort (Context context) {

        Toast.makeText(context, context.getString(R.string.toast_memoryshort), Toast.LENGTH_LONG).show();
        return;
        }

Change to

 public static void ToastMemoryShort (Context context) {

        Toast.makeText(context, context.getString(R.string.toast_memoryshort), Toast.LENGTH_LONG).show();
        return;
        }
伪心 2024-12-10 02:57:11

只需使用这个即可:

makeText(Context context, int resId, inturation) 制定标准
仅包含一个文本视图,其中包含来自资源的文本。

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

Just use this instead:

makeText(Context context, int resId, int duration) Make a standard
toast that just contains a text view with the text from a resource.

From http://developer.android.com/reference/android/widget/Toast.html

怪我入戏太深 2024-12-10 02:57:11

您可以使您的 toast 更加通用,如下所示:

public void toast(String msg){
    Context context = getApplicationContext();
    CharSequence text = msg;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

然后只需在需要时调用,如下所示:

toast( "My message hardcoded" );

或通过引用 strings.xml,如下所示:

toast( this.getString(R.string.toast_memoryshort) );

You could make your toast more generic like this:

public void toast(String msg){
    Context context = getApplicationContext();
    CharSequence text = msg;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

Then just call when you need like this:

toast( "My message hardcoded" );

or by referring to strings.xml like this:

toast( this.getString(R.string.toast_memoryshort) );
空气里的味道 2024-12-10 02:57:11

使用以下代码获得所需的输出:

Toast.makeText(getApplicationContext(),getString(R.string.exit_survey_toast),Toast.LENGTH_LONG).show();

exit_survey_toast 替换为您的字符串值。

Use the below code to get the desired output:

Toast.makeText(getApplicationContext(),getString(R.string.exit_survey_toast),Toast.LENGTH_LONG).show();

replace exit_survey_toast with your string value.

多像笑话 2024-12-10 02:57:11

您应该更改

CharSequence text = getString(R.string.toast_memoryshort); //error here

为:

CharSequence text = context.getString(R.string.toast_memoryshort);

getString 函数在 Context#getString(int)

You should change

CharSequence text = getString(R.string.toast_memoryshort); //error here

for:

CharSequence text = context.getString(R.string.toast_memoryshort);

The getString function is implemented in Context#getString(int)

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