1
我想传递一个方法(SaveClound)作为参数(AlertDialog参数),所以我可以通过该参数(在actionButtons方法)使用型动物的方法。如何在Android中将方法作为参数传递?
public void actionButtons(){
buttonVoltar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog(saveClound());
// see? I want to call the a method through this parameter
}
});
}
public void alertDialog(Method methodName) {
AlertDialog.Builder builderaction = new AlertDialog.Builder(this);
builderaction.setTitle("Atenção!");
builderaction.setMessage("Você tem certeza que deseja sair?");
builderaction.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// i want to call here the paramater i'm passing on this method (methodName)
// so i can call any methods i want right here
}
});
builderaction.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builderaction.create();
alert.setIcon(R.drawable.ic_stop);
alert.show();
}
public void saveClound(){
Toast.makeText(getApplicationContext(), "ABC", Toast.LENGTH_SHORT).show();
}
如果你试图传递的方法作为参数做到这一点,就不可能本身在Java中。有些方法可以通过Interfaces来完成。检查这个SO问题。 http://stackoverflow.com/questions/2186931/java-pass-method-as-parameter – JDenais
我看到了,但我无法理解它..:/ –
看看我的[回复](https:// stackoverflow.com的.com /问题/ 16800711 /传递功能作为一种参数合的java/46933426#46933426) –