首页 > 移动平台 > 详细

Android AlertDialog对话框回调

时间:2015-05-19 16:47:35      阅读:238      评论:0      收藏:0      [点我收藏+]

封装的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();
}
});


Android AlertDialog对话框回调

原文:http://670176656.blog.51cto.com/4500575/1652846

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!