从资源串Toast.makeText [英] Toast.makeText from resource string

查看:118
本文介绍了从资源串Toast.makeText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyPrimaryClass类,这个类有当pressed按钮女巫,创建与类myClassForResult的意图。

我使用它来启动它:

  startActivityForResult(myIntentOfMyClassForResult,ACTIVITY_EDIT_BTEXT);
 

无论MyPrimaryClass,并myClassForResult扩展了活动。

所以,当我打电话Toast.makeText的myClassForResult内,与R.string.my_resource_string的文本参数,它给了我力量关闭!

我已经试过这样:

 上下文C = myClassForResult.this;
吐司面包= Toast.makeText(C,
    c.getResources()的getString(R.string.my_resource_string)
    Toast.LENGTH_SHORT);
toast.show();
 

另外这款: C = getApplicationContext()

另外这款: C = getBaseContext()

还有这样的:

 上下文C = MyPrimaryClass.this;
吐司面包= Toast.makeText(C,
    R.string.my_resource_string,
    Toast.LENGTH_SHORT);
toast.show();
 

如果我用一个内联字符串,如我的文字干杯!,它的工作原理。但我需要得到的资源字符串。

- 问题解决了:

要解决,我改变了敬酒的持续时间Toast.LENGTH_LONG问题

字符串 R.string.my_resource_string 值的标题是空的

当我将其值改为称号,它工作正常,所以我猜的字符串是太长了Toast.LENGTH_SHORT时间。

但是,当我改变持续时间为 Toast.LENGTH_LONG ,我可以使用很长的字符串。

 上下文C = MyPrimaryClass.this;
吐司面包= Toast.makeText(C,
    R.string.my_resource_string,
    Toast.LENGTH_LONG);
toast.show();
 

解决方案

有一点需要注意:

 吐司面包= Toast.makeText(C,
    c.getResources()的getString(R.string.my_resource_string)
    Toast.LENGTH_SHORT);
toast.show();
 

可以简化为:

  Toast.makeText(C,
    c.getResources()的getString(R.string.my_resource_string)
    Toast.LENGTH_SHORT).show();
 

这可以节省你,你并不需要一个对象引用。

您需要了解的一件事是,当你引用你在你的包r(不android.R),你将有机会获得你的资源,只要你有语境。

更新

意识到你正在使用此为我以后会建议您 改变你的方法,而这实际上是可能的,你的做法是不理想的东西这么简单。

该方法startActivityForResult(XX),通常是当你要启动一个应用程序,你的包之外的结果。

例如:如果我想检索产品酒吧code,然后我会开始的意图是吧code类的间接通过一个动作。然后,我通过使用onActivityResult(XX)检索数据。

这让否感,以便为自己的类做到这一点。

I have a class named MyPrimaryClass, this class has a button witch when pressed, creates an Intent with the class myClassForResult.

I use this to start it:

startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);

Both MyPrimaryClass, and myClassForResult extends Activity.

So, when I call Toast.makeText within the myClassForResult, with the text parameter of R.string.my_resource_string, it gives me Force Close!

I have tried this:

Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT);
toast.show();

Also this: c = getApplicationContext()

Also this: c = getBaseContext()

Also this:

Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
    R.string.my_resource_string,
    Toast.LENGTH_SHORT);
toast.show();

If I use an inline string, like "My toast Text!", it works. But i need to get a string from the resources.

-Problem solved:

To solve the problem I changed the duration of the Toast to Toast.LENGTH_LONG

The string R.string.my_resource_string value is "The title is empty"

When I change its value to "The title", it worked properly, so I guess the string was too long for the Toast.LENGTH_SHORT duration.

But when i change the duration to Toast.LENGTH_LONG, I could use the long string.

Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
    R.string.my_resource_string,
    Toast.LENGTH_LONG);
toast.show();

解决方案

One thing to note:

Toast toast = Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT);
toast.show();

Can be simplified into:

Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT).show();

This saves you an object reference that you do not need.

One thing you need to understand is that whenever you reference you R in your package (not android.R.) you will have access to your resources as long as you have Context.

Update

After realizing what you are using this for I would recommend that you change your approach, while this is in fact possible, your approach isn't ideal for something so simple.

The method startActivityForResult(xx) is typically when you want to start an application that is outside of your package for a result.

For instance: if I wanted to retrieve a barcode from a product, then I'd start an Intent to that barcode class, indirectly through an action. Then I'd retrieve the data through using onActivityResult(xx).

it makes No Sense to do this for your own classes.

这篇关于从资源串Toast.makeText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆