从另一个类调用 Activity 类中的公共方法?
主要活动
public class MyActivity() extends Activity
{
onCreate()
{
MyClass myobj=new MyClass();
}
public void Mymethod()
{}
}
//HELPER CLASS IN A SEPARATE FILE
public class MyClass()
{
MyClass(Context context)
{
}
}
我尝试从 MyClass 的实例调用 Mymethod()。 我真的很感激任何帮助。谢谢。
MAIN ACTIVITY
public class MyActivity() extends Activity
{
onCreate()
{
MyClass myobj=new MyClass();
}
public void Mymethod()
{}
}
//HELPER CLASS IN A SEPARATE FILE
public class MyClass()
{
MyClass(Context context)
{
}
}
I tried to call Mymethod() from an instance of MyClass.
I would really appreciate any help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为什么不直接将活动传递给构造函数,例如
Why not just pass the activity to the constructor like
将该方法设为静态,这样您就可以在不创建类对象的情况下进行调用
,并像这样调用
Make that method as static so you can call without creating the class object
and call like this way
这可能是最好的方法。我就是这样做的。它称为单例设计模式:
This is probably the best way to do it. This is how I'm doing it. It's called a Singleton Design Pattern:
如果我理解正确,我相信您可以使用接口作为回调来解决您的问题。
////活动////////////////////////////////
//////自定义类//// ///////////////////////////
If I'm understanding you correctly I believe you can solve your problems using an interface as a callback.
////ACTIVITY/////////////////////////////////
//////Custom Class//////////////////
我有一个内部类,我想将其放入更通用的库“Helper”类中。我和你有同样的问题。我通过使用单个抽象方法将辅助类抽象化来解决这个问题。然后在我的项目包中,我通过特定类中的构造函数调用来扩展辅助类。
通过这种方式,我有一个包含绝大多数“工作”的帮助程序类,我所要做的就是使用构造函数和一个方法创建一个子类,以便访问调用活动的感兴趣的方法。
I had an inner class that I wanted to pull out into a more general library "Helper" class. I had the same issue you do. I got around it by making the helper class abstract, with a single abstract method. Then in my project package I extended the helper class with a constructor call in the specific class.
In this way, I have a helper class that contains the vast majority of the "work", and all I have to do is make a subclass with the constructor and one method in order to get access to the calling activity's method of interest.
您必须将 MainActivity 的实例传递到另一个类中,然后您可以从任何地方调用所有公共内容(在 MainActivity 中)。
MainActivity.java
AnotherClass.java
You have to pass instance of MainActivity into another class, then you can call everything public (in MainActivity) from everywhere.
MainActivity.java
AnotherClass.java
在MainActivity.class文件中
您必须从 MainActivity 类传递 MainActivity 上下文。然后在 MyClass 中你必须获取 MainActivity 上下文。请记住 Context 和 MyActivity 是两个不同的引用。
//帮助类位于单独的文件中
In MainActivity.class file
You have to pass MainActivity context from MainActivity Class. Then in MyClass you have to Get MainActivity context. Remember Context and MyActivity are two different reference.
//HELPER CLASS IN A SEPARATE FILE
我决定将 HelperClass MyClass 编写为 MyActivity 类的内部类。这允许它完全访问父类,但不好的是现在 MyClass 仅限于 MyActivity 类。
I decided to write the HelperClass MyClass as an inner class of MyActivity class. This allows it full access to parent class but the bad thing is now MyClass is restricted to MyActivity class only.