如何从正在运行的活动(running activity)中的普通Java类中打印出内容?

huangapple go评论67阅读模式
英文:

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。有几种方法可以实现:

  1. MultiplayerActivity.onCreate()方法中创建Game.class的实例,并将MultiplayerActivity的指针(this)作为参数传递给Game.class的构造函数。

  2. Game.class中声明一个公共的静态指针,类型为MultiplayerActivity,并在MultiplayerActivityonCreate()方法中直接设置MultiplayerActivity的指针(this)作为值(不安全),或者通过静态的setter方法进行设置。

  3. 或者反过来:在MultiplayerActivityonCreate()方法中将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:

  1. Create instance of Game.class in .onCreate() method of MultiplayerActivity and pass MultiplayerActivity's pointer (this) to Game.class constructor as a parameter.

  2. Declare public static pointer of MultiplayerActivity type in Game.class and set MultiplayerActivity's pointer (this) as value in onCreate() method of MultiplayerActivity directly (not safe) or through static setter method.

  3. Or vice versa: save MultiplayerActivity's pointer in onCreate() method in static variable and provide public static getter method.

huangapple
  • 本文由 发表于 2020年8月31日 20:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定