我有一个泛型类女巫会弹出一个简单的对话框:
public class GenericDialogPopUp {
public static void genericCreatePopUp(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("message body");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do if ok is pressed
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do if cancel is pressed
}
});
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
}
}
我需要在Fragment / Activity中调用此方法:
GenericDialogPopUp.genericCreatePopUp(getActivity()); / GenericDialogPopUp.genericCreatePopUp(SomeActivity.this);
问题是我想创建一种监听器来知道是否按下了对话框中的确定按钮,然后在这个中执行这些操作(我在这里调用对话框)类,如:
if(ok_bt)
private void doPositive(){ //code }
else
private void doNegative(){ //code }
答案 0 :(得分:1)
您的genericCreatePopUp
方法如下
public static void genericCreatePopUp(Context context, final SimpleListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("message body");
builder.setPositiveButton(okTxt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onPosBtnClick();
}
});
builder.setNegativeButton(bt_can, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onNegBtnClick();
}
});
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
}
SimpleListener接口:
interface SimpleListener {
void onPosBtnClick();
void onNegBtnClick();
}
从Fragment / Activity中调用它:
genericCreatePopUp(context, new SimpleListener (){
@Override
onPosBtnClick(){
// your code
}
@Override
onNegBtnClick(){
// your code
}
});