封装的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(); } });
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。