android dialog的封装,Android Dialog 简单封装
轉(zhuǎn)載:https://www.cnblogs.com/zjjne/archive/2013/10/03/3350382.html
public class MyAlertDialog {
//region 確認(rèn)/取消 彈出框
//取消按鈕,默認(rèn)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);
//設(shè)置對(duì)話(huà)框標(biāo)題
builder.setTitle(title);
//設(shè)置對(duì)話(huà)框消息
builder.setMessage(message);
//設(shè)置確定按鈕
builder.setPositiveButton(positiveBtnName, positiveBtnListener);
//設(shè)置取消按鈕
builder.setNegativeButton(negativeBtnName, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
//創(chuàng)建一個(gè)消息對(duì)話(huà)框
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);
//設(shè)置對(duì)話(huà)框標(biāo)題
builder.setTitle(title);
//設(shè)置對(duì)話(huà)框消息
builder.setMessage(message);
//設(shè)置確定按鈕
builder.setPositiveButton(positiveBtnName, positiveBtnListener);
//設(shè)置取消按鈕
builder.setNegativeButton(negativeBtnName, negativeBtnListener);
//創(chuàng)建一個(gè)消息對(duì)話(huà)框
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);
//設(shè)置對(duì)話(huà)框標(biāo)題
builder.setTitle(title);
builder.setSingleChoiceItems(ss, 1, btnListener);
//創(chuàng)建一個(gè)消息對(duì)話(huà)框
dialog = builder.create();
return dialog;
}
//endregion
}
調(diào)用方式:
點(diǎn)擊btn按鈕時(shí),彈出對(duì)話(huà)框。
確認(rèn)后,執(zhí)行你的方法();
調(diào)用確認(rèn)框
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = MyAlertDialog.createConfirmDialog(InboundPOActivity.this, "提交", "入庫(kù)確認(rèn)", "確定", "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
你的方法();
}
});
dialog.show();
}
});
調(diào)用單選框
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();
總結(jié)
以上是生活随笔為你收集整理的android dialog的封装,Android Dialog 简单封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C语言两班平均成绩,用C语言编程平均分数
- 下一篇: 声音分析+android,声音分析仪So