Android AlertDialog对话框回调

发布时间:2020-06-05 09:42:21 作者:671076656
来源:网络 阅读:1760

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


推荐阅读:
  1. Android中容易忽略的知识
  2. Android仿微信录音功能

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

回调 android dialog

上一篇:管理信息系统测试方法总结(八)

下一篇:etcd和flannel实现docker多网段通信

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》