【问题标题】:How can i pass a function as a parameter to another function in android?如何将函数作为参数传递给android中的另一个函数?
【发布时间】:2012-10-08 23:09:14
【问题描述】:

那么如何将一个函数作为参数传递给另一个函数,例如我想传递这个函数:

public void testFunkcija(){
    Sesija.forceNalog(reg.getText().toString(), num);
}

在这个:

    public static void dialogUpozorenjaTest(String poruka, Context context, int ikona, final Method func){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
        alertDialogBuilder.setTitle("Stanje...");
        alertDialogBuilder
            .setMessage(poruka)
            .setIcon(ikona)
            .setCancelable(true)                        
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    //here
                }
              });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
}

【问题讨论】:

  • 函数不是 Java 中的第一类对象。除此之外,你为什么不能简单地在你需要的地方调用你想要的方法呢?
  • 为此使用接口。一些例子:javaworld.com/javatips/jw-javatip10.html
  • 创建方法所属类的对象,并在对话框中使用对象引用简单地调用它
  • 查看我的answer

标签: java android methods parameters


【解决方案1】:

您可以使用 Runnable 来包装您的方法:

Runnable r = new Runnable() {
    public void run() {
        Sesija.forceNalog(reg.getText().toString(), num);
    }
}

然后将它传递给你的方法并在你需要的地方调用r.run();

public static void dialogUpozorenjaTest(..., final Runnable func){
    //.....
        .setPositiveButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                func.run();
            }
          });
}

【讨论】:

  • 如果我需要返回值怎么办?
  • @nmtuan 使用可调用对象?
【解决方案2】:

好吧,因为在 Java 中没有代表(哦 C#,我好想你),你可以这样做的方法是创建一个实现接口的类,可能是可运行的或一些自定义接口,然后你可以调用你的方法通过界面。

【讨论】:

    【解决方案3】:

    函数本身不能直接传递。您可以使用interface 实现作为回调机制来进行调用。

    界面:

    public interface MyInterface {
    
       public void testFunkcija();
    }   
    

    实施:

    public class MyInterfaceImpl implements MyInterface 
       public void testFunkcija(){
           Sesija.forceNalog(reg.getText().toString(), num);
       }
    }
    

    并根据需要将MyInterfaceImpl 实例传递给它:

    public static void dialogUpozorenjaTest(MyInterface myInterface, ...)
    
       myInterface.testFunkcija();
       ...
    

    【讨论】:

      【解决方案4】:

      最简单的方法是使用runnable 让我们看看如何

      //this function can take function as parameter 
      private void doSomethingOrRegisterIfNotLoggedIn(Runnable r) {
          if (isUserLoggedIn())
              r.run();
          else
              new ViewDialog().showDialog(MainActivity.this, "You not Logged in, please log in or Register");
      }
      

      现在让我们看看如何将任何函数传递给它(我不会使用 lambda 表达式)

      Runnable r = new Runnable() {
                      @Override
                      public void run() {
                          startActivity(new Intent(MainActivity.this, AddNewPostActivity.class));
                      }
                  };
      doSomethingOrRegisterIfNotLoggedIn(r);
      

      让我们传递另一个函数

      Runnable r = new Runnable() {
                      @Override
                      public void run() {
                          if(!this.getClass().equals(MyProfileActivity.class)) {
                              MyProfileActivity.startUserProfileFromLocation( MainActivity.this);
                              overridePendingTransition(0, 0);
                           }
                      }
                  };
      doSomethingOrRegisterIfNotLoggedIn(r);
      

      就是这样。快乐的大思考...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 2019-10-09
        • 2019-08-19
        • 1970-01-01
        • 2023-01-12
        • 2013-04-13
        • 2013-11-19
        相关资源
        最近更新 更多