全局变量 - 从 View 类访问

发布于 2024-11-30 02:36:02 字数 544 浏览 1 评论 0原文

有一点问题。不幸的是我在工作(不在家)所以无法显示代码。

基本上,我有三个类文件 - 我的主类,目前正在处理触摸屏功能 - 一个行为类,它是一个扩展视图并由主类调用(在屏幕上绘制) - 和一个全局类,其中包含全局变量、设置和检索它们的所有函数等。

现在,在遵循一些教程和论坛答案后,我已经让全局变量正常工作了。该文件在清单中声明,并且在主类(我认为是扩展应用程序)中工作正常(使用所有 getApplicationContext 内容)。

然而,我确实需要让它们在 act 类(这是一个扩展视图)中工作,但它们不是。我认为这显然是因为这是一个扩展视图,而我似乎对此无能为力。

有人有什么想法吗?

谢谢。

对于回复(因为我是新手,所以还不能回答,而且评论似乎不想与 IE6 一起工作 - 我们的 IT 部门很糟糕)

@David Olsson - 谢谢,当我加入时会尝试一下。

@Aioobe - 呵呵,不,并不是很着急。应该是昨晚发的通常是相反的,我在家,不工作,所以无法显示代码......

如果你在我家,你能检查一下狗吗?

Having a bit of a problem. Unfortunately I'm at work (not home) so cannot show code.

Basically, I've got three class files - my main class, which at the moment is handling the touchscreen functionality - an act class, which is an extends View and is called by the main class (draws on the screen) - and a global class, which contains global variables, all the functions to set and retrieve them etc.

Now, I've got the global variables working okay after following some tutorials and forum answers. The file is declared in the manifest and, in the main class (which I think is an extends application) works fine (using all the getApplicationContext stuff).

However, I really need to get them to work in the act class (which is an extends View) but they're not. I gather this is obviously because this is an extends View which is something it doesn't seem I can do anything about.

Anyone got any ideas?

Thanks.

For replies (as I'm a newb so can't answer yet, and comments don't seem to want to work with IE6 - our IT dept sucks)

@David Olsson - thanks, will give that a go when I get in.

@Aioobe - heh, no, not really in much of a hurry. Should've posted last night. Normally it's the other way round, I'm at home, not work so can't show code...

And if you are at my home can you check on the dog?

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

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

发布评论

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

评论(1

冷…雨湿花 2024-12-07 02:36:02

您始终可以使用放置全局变量引用的方法。
IE 在你的视图类中:

private MyGlobalVariables myVariables;
public void setGlobalVariable(MyGlobalVariables variables) { 
    myVariables = variables;
}

然后在你的活动类中我猜你有一个对你的视图的引用,如果你不只是创建一个并使用

myView.setGlobalVariable(myVariables);

You could always use a method where you put the reference for the global variables.
IE in your View class:

private MyGlobalVariables myVariables;
public void setGlobalVariable(MyGlobalVariables variables) { 
    myVariables = variables;
}

and then in your activity class I guess you have a reference to your View, if you don't just create one and use

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