java 如何将函数作为参数传递给android中的另一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12955558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i pass a function as a parameter to another function in android?
提问by nexusone
So how can I pass a function as a parameter to another function, for example i want to pass this function:
那么如何将一个函数作为参数传递给另一个函数,例如我想传递这个函数:
public void testFunkcija(){
Sesija.forceNalog(reg.getText().toString(), num);
}
in this:
在这个:
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();
}
回答by assylias
You can use a Runnable to wrap your method:
您可以使用 Runnable 来包装您的方法:
Runnable r = new Runnable() {
public void run() {
Sesija.forceNalog(reg.getText().toString(), num);
}
}
Then pass it to your method and call r.run();
where you need it:
然后将其传递给您的方法并r.run();
在需要的地方调用:
public static void dialogUpozorenjaTest(..., final Runnable func){
//.....
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
func.run();
}
});
}
回答by HericDenis
Well, since there are no dellegates in Java (oh C# I miss you so bad), the way you can do it is creating a class that implements a interface, maybe runnable or some custom interface and than you can call your method through the interface.
好吧,由于 Java 中没有委托(哦,C# 我很想念你),你可以这样做的方法是创建一个实现接口的类,可能是可运行的或一些自定义接口,然后你可以通过接口调用你的方法.
回答by Reimeus
Functions cannot be passed directly themselves. You could use an interface
implementation as a callback mechanism to make the call.
函数本身不能直接传递。您可以使用interface
实现作为回调机制来进行调用。
Interface:
界面:
public interface MyInterface {
public void testFunkcija();
}
Implementation:
执行:
public class MyInterfaceImpl implements MyInterface
public void testFunkcija(){
Sesija.forceNalog(reg.getText().toString(), num);
}
}
and pass it a MyInterfaceImpl
instance as required to:
并MyInterfaceImpl
根据需要将实例传递给:
public static void dialogUpozorenjaTest(MyInterface myInterface, ...)
myInterface.testFunkcija();
...
回答by Basheer AL-MOMANI
the simplest way is using runnable
let's see how
最简单的方法是使用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");
}
now let's see how can I pass any function it it (I will not use lambda expression)
现在让我们看看如何将任何函数传递给它(我不会使用 lambda 表达式)
Runnable r = new Runnable() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this, AddNewPostActivity.class));
}
};
doSomethingOrRegisterIfNotLoggedIn(r);
let's pass another function
让我们传递另一个函数
Runnable r = new Runnable() {
@Override
public void run() {
if(!this.getClass().equals(MyProfileActivity.class)) {
MyProfileActivity.startUserProfileFromLocation( MainActivity.this);
overridePendingTransition(0, 0);
}
}
};
doSomethingOrRegisterIfNotLoggedIn(r);
thas's it. happy big thinking...
就是这样。快乐的大思想...