在另一个活动中使用一个活动的方法
我有一些游戏的“主要”活动,并且我创建了一个类来包含将在各种主要活动中使用的所有方法。
一种方法是这样的:
public void printMessage(String message, int time)
{
Toast toastMessage = Toast.makeText(this, "", Toast.LENGTH_SHORT);
toastMessage.setText(message);
toastMessage.setDuration(time);
toastMessage.show();
}
显然,这种方法需要类扩展 Activity,所以我猜这就是问题所在。
我读到了有关使用 Intent 和 putExtra、getExtra...的信息,但在这种情况下这将是一种矫枉过正。 我知道这个 printMessage 并不能真正证明新类的合理性,但我有更多的方法可以这样做,并且这些方法也需要 Activity。
我不得不提的是,我对 Android 和 Java 都非常陌生。现在我已经习惯了 Java 创建对象然后对该对象进行方法调用的方式。这就是我在这里实际尝试的方法,但它仅适用于“常规课程”,不适用于活动课程。
谢谢。
I have a few "main" activities for the game and I have created a class to include all the methods that are going to be used in various main activities.
One method is this:
public void printMessage(String message, int time)
{
Toast toastMessage = Toast.makeText(this, "", Toast.LENGTH_SHORT);
toastMessage.setText(message);
toastMessage.setDuration(time);
toastMessage.show();
}
Obviously, this method requires the class to extend Activity, so I'm guessing that is the problem.
I read about using intents and putExtra, getExtra... but that would be an overkill in this case.
I know this printMessage doesn't really justify a new class, but I have more methods that do and those require Activity as well.
I have to mention that I'm extremely new to Android and new to Java. Right now I'm used to Java's way of creating an object and then do a method call with that object. This is how I actually tried here, but it only works with "regular classes", not with activity classes.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该类不需要扩展 Activity,只需通过添加这样的额外参数将调用 Activity 的上下文传递给 printMessage 方法...
编辑:响应您的评论...
来自据我所知,您创建了一个“帮助程序”类,它不是特定于 Android 的,但需要使用 Android 元素(例如活动上下文、SharedPreferences 等)。在许多情况下,这些东西可以按照我上面演示的方式传递到辅助类的各种方法中(没有问题)。
好的,很明显,在前一个 Toast 超时之前它会被重复调用。尝试使“toastMessage”成为您的帮助程序类的实例成员...
同样,“helper”类的任何方法都可以传递给调用它们的任何 Activity 的 Context,这可用于获取首选项等。
或者,正如 FvZ 所提到的,您可以扩展 Application 类并提供对应用程序范围共享首选项的访问...
然后所有活动都可以简单地使用(例如)...
在这种情况下,您还可以将帮助器类创建为应用程序的静态成员,使其可以从所有活动访问。
需要掌握的一件事是,Android 确实使用“常规”Java...只是 Android Activity 类之类的东西是一种非常特殊的情况。当一个 Activity 覆盖另一个 Activity 时,您无法保证第一个 Activity 会存活多久。在许多情况下,它会很快被销毁 - 因此,您不能依赖能够从一个 Activity 调用另一个 Activity 中的方法。这也不是 Android 应该的工作方式。
The class doesn't need to extend Activity, just pass the Context of the calling Activity to the printMessage method by adding an extra parameter like this...
EDIT: In response to your comment...
From what I can tell, you've created a 'helper' class which isn't Android specific but needs to use Android elements (such as the Context of Activities, SharedPreferences etc). In many cases these things can be passed into the various methods of the helper class (without problem) in the way I demonstrated above.
OK, so obviously it's being called repeatedly before the previous Toast times out. Try making 'toastMessage' an instance member of your helper class...
Again, any methods of your 'helper' class can be passed the
Context
of any Activity which calls them and this can be used for getting preferences etc.Alternatively, as mentioned by FvZ you can extend the Application class and provide access to the application-wide Shared Preferences...
All Activities can then simply use (for example)...
In this case, you could also create your helper class as a static member of your Application making it accessible from all Activities.
One thing to grasp is that Android does use 'regular' Java...it's just that something like the Android Activity class is a very special case. When one Activity covers another, you have no guarantee how long the first Activity will stay alive. In many cases it will be destroyed very quickly - for that reason, you can't rely on being able to call a method in one Activity from another. It's also not the way that Android is supposed to work.
另一种方法是扩展您的应用程序,这将为您提供一个持久存在于所有应用程序中的对象,然后您可以将指向您的活动的指针传递给您需要的所有其他活动
Another way is to extend you application, that will give you a object that will persist to all the application, then you can pass a pointer to your activity to all the others activities that you need