【问题标题】:How can I pass a Method as a parameter in Android?如何在 Android 中将方法作为参数传递?
【发布时间】:2015-08-19 22:18:17
【问题描述】:

我想传递一个方法(SaveClound)作为参数(AlertDialog Parameter),所以我可以通过这个参数使用不同的方法(在actionButtons方法中)。

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 中是不可能的。有很多方法可以通过接口来做到这一点。检查这个 SO 问题。 stackoverflow.com/questions/2186931/…
  • 我看到了,但我看不懂.. :/
  • 查看我的answer

标签: android methods parameters


【解决方案1】:

你可以通过传递一个runnable给方法来做到这一点,例如

public void actionButtons(){
    buttonVoltar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                   saveClound();
               }
            };

            alertDialog(runnable);
        }
    });
}

public void alertDialog(Runnable runnable) {
    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
                new Handler().post(runnable);
            }
        });
    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(getActivity(), "ABC", Toast.LENGTH_SHORT).show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 2010-10-16
    • 2013-08-10
    • 2021-04-17
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多