我想传递一个方法( SaveClound )作为参数( AlertDialog参数),这样我就可以通过此参数使用不同的方法( in actionButtons Method < /强>)。
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();
}
答案 0 :(得分: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();
}