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();
總結
以上是生活随笔為你收集整理的android dialog的封装,Android Dialog 简单封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言两班平均成绩,用C语言编程平均分数
- 下一篇: android 设置默认铃声,Andro