封装的Dialog对话框,有时候需要获取选项这时候就用到了函数的回调package com.example.testlistq;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
public class MyDialog {
private static AlertDialog dialog;
//回调接口
public interface ResultCallBack{
public void callback(boolean flag);
}
public static void showDialog(Context context, String str, final ResultCallBack mRCallBack){
dialog = new AlertDialog.Builder(context)
.setTitle("提示")
.setMessage(str)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(mRCallBack != null){
mRCallBack.callback(true);
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(mRCallBack != null){
mRCallBack.callback(false);
}
}
}).create();
dialog.setCancelable(false);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {//屏蔽搜索键
return true;
} else {
return false; //默认返回 false
}
}
});
dialog.show();
}
}
//调用实例MyDialog.showDialog(this, "回调窗口", new ResultCallBack() {
@Override
public void callback(boolean flag) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "flag = " + flag, Toast.LENGTH_SHORT).show();
}
});
原文:http://670176656.blog.51cto.com/4500575/1652846