如何在Android中将一个函数作为参数传递给另一个函数?

11

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

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中的函数不是一等对象。除此之外,为什么不能在需要的地方直接调用所需的方法呢? - Makoto
使用接口来实现。以下是一些示例:http://www.javaworld.com/javatips/jw-javatip10.html - olshevski
创建一个属于该方法所在类的对象,并在对话框中使用对象引用来调用它。 - Hussain Akhtar Wahid 'Ghouri'
请查看我的答案 - Vasile Doe
4个回答

23
您可以使用 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();
            }
          });
}

如果我需要一个返回值呢? - rocketspacer
2
@nmtuan 使用Callable? - assylias

3

好的,由于Java中没有委托(哦,我非常想念C#),您可以创建一个实现接口的类,可能是Runnable或某些自定义接口,然后通过接口调用您的方法。


2

函数本身不能直接传递。您可以使用 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();
   ...

1

最简单的方法是使用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);

这就是它。祝你有愉快的大思考...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接