android dialog的封装,Android Dialog 简单封装

转载:https://www.cnblogs.com/zjjne/archive/2013/10/03/3350382.html

public class MyAlertDialog {

//region 确认/取消 弹出框

//取消按钮,默认canel

public static Dialog createConfirmDialog(Context context, String title, String message,

String positiveBtnName, String negativeBtnName, DialogInterface.OnClickListener positiveBtnListener) {

Dialog dialog = null;

AlertDialog.Builder builder = new AlertDialog.Builder(context);

//设置对话框标题

builder.setTitle(title);

//设置对话框消息

builder.setMessage(message);

//设置确定按钮

builder.setPositiveButton(positiveBtnName, positiveBtnListener);

//设置取消按钮

builder.setNegativeButton(negativeBtnName, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

});

//创建一个消息对话框

dialog = builder.create();

return dialog;

}

//自定义取消按钮事件

public static Dialog createConfirmDialog(Context context, String title, String message,

String positiveBtnName, String negativeBtnName, DialogInterface.OnClickListener positiveBtnListener,

DialogInterface.OnClickListener negativeBtnListener) {

Dialog dialog = null;

AlertDialog.Builder builder = new AlertDialog.Builder(context);

//设置对话框标题

builder.setTitle(title);

//设置对话框消息

builder.setMessage(message);

//设置确定按钮

builder.setPositiveButton(positiveBtnName, positiveBtnListener);

//设置取消按钮

builder.setNegativeButton(negativeBtnName, negativeBtnListener);

//创建一个消息对话框

dialog = builder.create();

return dialog;

}

//endregion

//region 单选 弹出框

public static Dialog createRadioDialog(Context context, String title, final String[] ss , DialogInterface.OnClickListener btnListener) {

Dialog dialog = null;

AlertDialog.Builder builder = new AlertDialog.Builder(context);

//设置对话框标题

builder.setTitle(title);

builder.setSingleChoiceItems(ss, 1, btnListener);

//创建一个消息对话框

dialog = builder.create();

return dialog;

}

//endregion

}

调用方式:

点击btn按钮时,弹出对话框。

确认后,执行你的方法();

调用确认框

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Dialog dialog = MyAlertDialog.createConfirmDialog(InboundPOActivity.this, "提交", "入库确认", "确定", "取消",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

你的方法();

}

});

dialog.show();

}

});

调用单选框

final String[] ss={"1","2","3"};

Dialog dialog = MyAlertDialog.createRadioDialog(InboundPOActivity.this,"Test",ss,new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

Toast.makeText(InboundPOActivity.this, "性别为:" + ss[which], Toast.LENGTH_SHORT).show();

}

});

dialog.show();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值