繁体   English   中英

我如何将一个函数作为参数传递给android中的另一个函数?

[英]How can i pass a function as a parameter to another function in android?

因此,如何将一个函数作为参数传递给另一个函数,例如,我想传递此函数:

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();
}

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

好吧,由于Java中没有委托(哦,C#,我很想你),您可以通过创建实现接口的类来实现,该类可以是可运行的或某些自定义接口,然后可以通过该接口调用方法。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM