从另一个类调用 Activity 类中的公共方法?

发布于 2024-12-11 12:36:18 字数 407 浏览 0 评论 0原文

主要活动

    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 技术交流群。

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

发布评论

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

评论(8

我的痛♀有谁懂 2024-12-18 12:36:18

为什么不直接将活动传递给构造函数,例如

public class MyActivity extends Activity { 

   onCreate(){ 
        MyClass myobj=new MyClass(MyActivity.this);     
   } 

   public void myMethod(){

   } 
} 

//HELPER CLASS IN A SEPARATE FILE     
public class MyClass{ 
    public MyClass(MyActivity act) { 
        act.myMethod();
    } 
} 

Why not just pass the activity to the constructor like

public class MyActivity extends Activity { 

   onCreate(){ 
        MyClass myobj=new MyClass(MyActivity.this);     
   } 

   public void myMethod(){

   } 
} 

//HELPER CLASS IN A SEPARATE FILE     
public class MyClass{ 
    public MyClass(MyActivity act) { 
        act.myMethod();
    } 
} 
没有你我更好 2024-12-18 12:36:18

将该方法设为静态,这样您就可以在不创建类对象的情况下进行调用

public static void Mymethod()
{}

,并像这样调用

MainActivity.Mymethod();

Make that method as static so you can call without creating the class object

public static void Mymethod()
{}

and call like this way

MainActivity.Mymethod();
叫思念不要吵 2024-12-18 12:36:18

这可能是最好的方法。我就是这样做的。它称为单例设计模式:

public class MyActivity extends Activity {
    private static MainActivity instance;

    public static MainActivity getInstance() {
     if(instance==null){
          setInstance(this);
        }
        return instance;
    }

    public static void setInstance(MainActivity instance) {
        MainActivity.instance = instance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setInstance(this);
    }
}

This is probably the best way to do it. This is how I'm doing it. It's called a Singleton Design Pattern:

public class MyActivity extends Activity {
    private static MainActivity instance;

    public static MainActivity getInstance() {
     if(instance==null){
          setInstance(this);
        }
        return instance;
    }

    public static void setInstance(MainActivity instance) {
        MainActivity.instance = instance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setInstance(this);
    }
}
美人迟暮 2024-12-18 12:36:18

如果我理解正确,我相信您可以使用接口作为回调来解决您的问题。

////活动////////////////////////////////

 public class MyActivity() extends Activity {

    onCreate()
    {
        MyClass myObj=new MyClass();   

        //Set the listener on the object. Created as anonymous
        myObj.setListener(new MyClass.Listener() {
             myMethod();
        });
    }
}

public void myMethod(){

}

//////自定义类//// ///////////////////////////

public class MyClass {
     Listener mListener;

     public interface Listener {
          public void onInterestingEvent();
     }

     public void setListener(Listener listener) {
          mListener = listener;
     }

     public void someUsefulThingTheClassDoes() {
          //Do your code here and when you're ready to call the activity's method do this
          mListener.onInterestingEvent();
     }
}

If I'm understanding you correctly I believe you can solve your problems using an interface as a callback.

////ACTIVITY/////////////////////////////////

 public class MyActivity() extends Activity {

    onCreate()
    {
        MyClass myObj=new MyClass();   

        //Set the listener on the object. Created as anonymous
        myObj.setListener(new MyClass.Listener() {
             myMethod();
        });
    }
}

public void myMethod(){

}

//////Custom Class//////////////////

public class MyClass {
     Listener mListener;

     public interface Listener {
          public void onInterestingEvent();
     }

     public void setListener(Listener listener) {
          mListener = listener;
     }

     public void someUsefulThingTheClassDoes() {
          //Do your code here and when you're ready to call the activity's method do this
          mListener.onInterestingEvent();
     }
}
不美如何 2024-12-18 12:36:18

我有一个内部类,我想将其放入更通用的库“Helper”类中。我和你有同样的问题。我通过使用单个抽象方法将辅助类抽象化来解决这个问题。然后在我的项目包中,我通过特定类中的构造函数调用来扩展辅助类。

public class MyActivity extends Activity {
    onCreate() {
        MyHelperClass = new MyHelperClass(this, "foobar");
    }

    public void myMethod() {
        // Code...
    }
}

// In a different file
public class MyHelperClass extends HelperClass {
    private MyActivity mInstance;

    public MyHelperClass(MyActivity act, String data) {
        super();
        this.mInstance = act;
        this.mActivity = act;  // Useful for calling generic Activity methods in the HelperClass
        this.mData = data;
    }

    protected void callMyActivityMethod() {
        mInstance.myMethod();
    }
}

// In a different file
public abstract class HelperClass {
    protected Activity  mActivity;
    protected String    mData;

    public HelperClass() {
        // Subclass will set variables
    }

    protected abstract void callMyActivityMethod();

    // More code for all the other stuff the class does
}

通过这种方式,我有一个包含绝大多数“工作”的帮助程序类,我所要做的就是使用构造函数和一个方法创建一个子类,以便访问调用活动的感兴趣的方法。

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.

public class MyActivity extends Activity {
    onCreate() {
        MyHelperClass = new MyHelperClass(this, "foobar");
    }

    public void myMethod() {
        // Code...
    }
}

// In a different file
public class MyHelperClass extends HelperClass {
    private MyActivity mInstance;

    public MyHelperClass(MyActivity act, String data) {
        super();
        this.mInstance = act;
        this.mActivity = act;  // Useful for calling generic Activity methods in the HelperClass
        this.mData = data;
    }

    protected void callMyActivityMethod() {
        mInstance.myMethod();
    }
}

// In a different file
public abstract class HelperClass {
    protected Activity  mActivity;
    protected String    mData;

    public HelperClass() {
        // Subclass will set variables
    }

    protected abstract void callMyActivityMethod();

    // More code for all the other stuff the class does
}

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.

岁月蹉跎了容颜 2024-12-18 12:36:18

您必须将 MainActivity 的实例传递到另一个类中,然后您可以从任何地方调用所有公共内容(在 MainActivity 中)。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    // Instance of AnotherClass for future use
    private AnotherClass anotherClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Create new instance of AnotherClass and
        // pass instance of MainActivity by "this"
        anotherClass = new AnotherClass(this);
    }

    // Method you want to call from another class
    public void myMethod(){
        ...
    }
}

AnotherClass.java

public class AnotherClass {

    // Main class instance
    private MainActivity mainActivity;  

    // Constructor
    public AnotherClass(MainActivity activity) {

        // Save instance of main class for future use
        mainActivity = activity;  

        // Call method in MainActivity
        mainActivity.myMethod();
    }
}

You have to pass instance of MainActivity into another class, then you can call everything public (in MainActivity) from everywhere.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    // Instance of AnotherClass for future use
    private AnotherClass anotherClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Create new instance of AnotherClass and
        // pass instance of MainActivity by "this"
        anotherClass = new AnotherClass(this);
    }

    // Method you want to call from another class
    public void myMethod(){
        ...
    }
}

AnotherClass.java

public class AnotherClass {

    // Main class instance
    private MainActivity mainActivity;  

    // Constructor
    public AnotherClass(MainActivity activity) {

        // Save instance of main class for future use
        mainActivity = activity;  

        // Call method in MainActivity
        mainActivity.myMethod();
    }
}
情话已封尘 2024-12-18 12:36:18

在MainActivity.class文件中
您必须从 MainActivity 类传递 MainActivity 上下文。然后在 MyClass 中你必须获取 MainActivity 上下文。请记住 Context 和 MyActivity 是两个不同的引用。

public class MyActivity extends Activity
{
    onCreate(){
        MyClass myobj=new MyClass(MyActivity context);    
    }
    public void Mymethod(){}
}

//帮助类位于单独的文件中

public class MyClass()
{
    MyActivity context;
    MyClass(MyActivity context)
    {
      this.context = context;

      this.context.Mymethod();
      //Or you can directly use activity context
      context.Mymethod();
    }
}

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.

public class MyActivity extends Activity
{
    onCreate(){
        MyClass myobj=new MyClass(MyActivity context);    
    }
    public void Mymethod(){}
}

//HELPER CLASS IN A SEPARATE FILE

public class MyClass()
{
    MyActivity context;
    MyClass(MyActivity context)
    {
      this.context = context;

      this.context.Mymethod();
      //Or you can directly use activity context
      context.Mymethod();
    }
}
执妄 2024-12-18 12:36:18

我决定将 HelperClass MyClass 编写为 MyActivity 类的内部类。这允许它完全访问父类,但不好的是现在 MyClass 仅限于 MyActivity 类。

public class MyActivity() extends Activity
{
    onCreate()
    { 
        MyClass myobj=new MyClass();

    } 

    public void myMethod()
    {

    } 
} 
//INNER CLASS
    public class MyClass
    { 
        public MyClass() 
        { 

        } 
        //I can directly access the MyMethod
        myMethod();
    }

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.

public class MyActivity() extends Activity
{
    onCreate()
    { 
        MyClass myobj=new MyClass();

    } 

    public void myMethod()
    {

    } 
} 
//INNER CLASS
    public class MyClass
    { 
        public MyClass() 
        { 

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