英文:
How to print something out from a normal Java class in a running activity?
问题
我正在使用Android Studio编写一款扑克游戏。
游戏显示在MultiplayerActivity
中。但游戏的逻辑位于普通的游戏类中。
在普通游戏类发生事件时,我该如何在MultiplayerActivity
中打印出一些信息?(例如通过弹出一个Toast)。
英文:
I am currently programming a card game with Android Studio.
The game is displayed at MultiplayerActivity
. But the game's logic is in the normal class game.
How may I in case of an event that happens in the normal game class,print out something in the MultiplayerActivity
? (by e.g. giving a toast).
答案1
得分: 1
你可以创建一个父类,然后让所有其他类继承该类,并且可以在任何地方显示提示。
class ParentClass extends Activity{
public void showToast(){}
}
class NormalPlayer extends ParentClass{
// 调用父类方法。
}
class Multiplayer extends ParentClass{
// 调用父类方法。
}
英文:
You can create a parent class and then inherit that class with all your other classes and show the toast from anywhere.
class ParentClass extends Activity{
public void showToast(){}
}
class NormalPlayer extends ParentClass{
// call methods of parent class.
}
class Multiplayer extends ParentClass{
// call methods of parent class.
}
答案2
得分: 0
你可以创建一个公共类,然后从另一个类中调用它,就像这样:
公共类
public class Constants
{
public static final String USERTYPE = "user_type";
public static final String FULLNAME = "fullname";
public static final String DOB = "dob";
public static final String GENDER = "gender";
}
在另一个类中这样访问:
sp.edit().putString(Constants.FULLNAME, list.getDevProject_ProjectId()).commit();
英文:
You can create a public class and then call it from another class like this:
Public class
public class Constants
{
public static final String USERTYPE = "user_type";
public static final String FULLNAME = "fullname";
public static final String DOB = "dob";
public static final String GENDER = "gender";
}
Access in another class like this:
sp.edit().putString(**ConstantSP.FULLNAME**,list.getDevProject_ProjectId()).commit();
答案3
得分: 0
如果我理解你的问题正确的话,要从Game.class
访问你的MultiplayerActivity
,你只需要在创建MultiplayerActivity
时将MultiplayerActivity
实例的指针传递给Game.class
。有几种方法可以实现:
-
在
MultiplayerActivity
的.onCreate()
方法中创建Game.class
的实例,并将MultiplayerActivity
的指针(this)作为参数传递给Game.class
的构造函数。 -
在
Game.class
中声明一个公共的静态指针,类型为MultiplayerActivity
,并在MultiplayerActivity
的onCreate()
方法中直接设置MultiplayerActivity
的指针(this)作为值(不安全),或者通过静态的setter方法进行设置。 -
或者反过来:在
MultiplayerActivity
的onCreate()
方法中将MultiplayerActivity
的指针保存在静态变量中,并提供一个公共的静态getter方法。
英文:
If I understand you correctly, to access your MultiplayerActivity
from Game.class
all you need is to pass MultiplayerActivity
instance pointer to Game.class
when MultiplayerActivity
is created. There are several ways to do it:
-
Create instance of
Game.class
in.onCreate()
method ofMultiplayerActivity
and passMultiplayerActivity
's pointer (this) toGame.class
constructor as a parameter. -
Declare public static pointer of
MultiplayerActivity
type inGame.class
and setMultiplayerActivity
's pointer (this) as value inonCreate()
method ofMultiplayerActivity
directly (not safe) or through static setter method. -
Or vice versa: save
MultiplayerActivity
's pointer inonCreate()
method instatic
variable and providepublic static
getter method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论