自定义Dialog

本文介绍了一个用于Android应用的对话框工具类实现细节,包括多种类型的对话框创建方法,如带有确认按钮、取消按钮的对话框及列表选择对话框等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class DialogUtil {
public static AlertDialog getAlertDialog(final Context ctx, String title, String msg) {
Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(msg);


return builder.create();
}


public static AlertDialog getAlertDialog(final Context ctx, String title, String msg,
DialogInterface.OnClickListener positiveListener) {
Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton(ctx.getResources().getString(R.string.str_setting_confirm), positiveListener);


return builder.create();
}


public static AlertDialog getAlertDialog(final Context ctx, String title, String msg,
DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton(ctx.getResources().getString(R.string.str_setting_confirm), positiveListener);


if (null != negativeListener) {
builder.setNegativeButton(ctx.getResources().getString(R.string.str_setting_cancel), negativeListener);
} else {
builder.setNegativeButton(ctx.getResources().getString(R.string.str_setting_cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}


return builder.create();
}


public static AlertDialog getTextAlertDialog(final Context ctx, String title, String msg, View view,
DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(msg);
builder.setView(view);
builder.setPositiveButton(ctx.getResources().getString(R.string.str_setting_confirm), positiveListener);


builder.setNegativeButton(ctx.getResources().getString(R.string.str_setting_cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});


return builder.create();
}


public static Dialog getDoubleButtonDialog(final Context ctx, String msg, Button.OnClickListener positiveListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_double);
Button confirm = (Button) dialog.findViewById(R.id.confirm_double_dialog);
Button cancel = (Button) dialog.findViewById(R.id.deny_double_dialog);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_double_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(positiveListener);


cancel.setOnClickListener(new Button.OnClickListener() {


@Override
public void onClick(View v) {
dialog.cancel();
}
});
return dialog;


}


public static Dialog getDoubleButtonDialog(final Context ctx, String msg, Button.OnClickListener positiveListener,
Button.OnClickListener negativeListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_double);
Button confirm = (Button) dialog.findViewById(R.id.confirm_double_dialog);
Button cancel = (Button) dialog.findViewById(R.id.deny_double_dialog);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_double_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(positiveListener);


cancel.setOnClickListener(negativeListener);
return dialog;


}


public static Dialog getDoubleButtonDialog(final Context ctx, String msg, String confirm_msg, String cancel_msg,
Button.OnClickListener positiveListener, Button.OnClickListener negativeListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_double);
Button confirm = (Button) dialog.findViewById(R.id.confirm_double_dialog);
Button cancel = (Button) dialog.findViewById(R.id.deny_double_dialog);
confirm.setText(confirm_msg);
cancel.setText(cancel_msg);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_double_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(positiveListener);


cancel.setOnClickListener(negativeListener);
return dialog;
}


public static Dialog getSingleButtonDialog(final Context ctx, String msg, Button.OnClickListener positiveListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_single);
Button confirm = (Button) dialog.findViewById(R.id.confirm_single_dialog);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_single_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(positiveListener);
return dialog;
}


public static Dialog getSingleButtonDialog(final Context ctx, String msg, String confirm_msg,
Button.OnClickListener positiveListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_single);
Button confirm = (Button) dialog.findViewById(R.id.confirm_single_dialog);
confirm.setText(confirm_msg);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_single_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(positiveListener);
return dialog;
}


public static Dialog getSingleButtonDialog(final Context ctx, String msg) {
try {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().getDecorView().setPadding(0, 0, 0, 0); // 灏辫兘澶熸按骞冲崰婊′簡
//add by rc at 2016-8-4
dialog.getWindow().setGravity(Gravity.BOTTOM);
//ead
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// bug161@2016-3-25
dialog.setContentView(R.layout.dialog_module_single);
Button confirm = (Button) dialog.findViewById(R.id.confirm_single_dialog);
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_single_dialog);
tv_msg.setText(msg);
confirm.setOnClickListener(new Button.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
return dialog;
} catch (Exception e) {
e.printStackTrace();


return null;
}
}


/*
* public static AlertDialog getViewDialog( final Context ctx, String title,
* String msg, View view ) { Builder builder = new AlertDialog.Builder( ctx
* ); if ( null != title) builder.setTitle(title); if ( null != msg)
* builder.setMessage(msg); if ( null != view) builder.setView(view);

* return builder.create(); }
*/


public static AlertDialog getListDialog(final Context ctx, String title, String msg, int items,
DialogInterface.OnClickListener listener) {
Builder builder = new AlertDialog.Builder(ctx);
if (null != title)
builder.setTitle(title);
if (null != msg)
builder.setMessage(msg);
builder.setItems(items, listener);


return builder.create();
}


// ningyb 20160314
public static Dialog getResendDialog(final Context ctx, String msg, String confirm_msg, String cancel_msg,
Button.OnClickListener positiveListener, Button.OnClickListener negativeListener) {
final Dialog dialog = new Dialog(ctx);
// Get the layout inflater
Window win = dialog.getWindow();
win.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
win.setGravity(Gravity.BOTTOM);
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
win.setAttributes(lp);
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_module_resend);
Button confirm = (Button) dialog.findViewById(R.id.confirm_double_dialog);
Button cancel = (Button) dialog.findViewById(R.id.deny_double_dialog);
if (confirm_msg != null && !"".equals(confirm_msg)) {
confirm.setText(confirm_msg);
}
if (cancel_msg != null && !"".equals(cancel_msg)) {
cancel.setText(cancel_msg);
}
TextView tv_msg = (TextView) dialog.findViewById(R.id.textview_double_dialog);
if (msg != null && !"".equals(msg)) {
tv_msg.setText(msg);
}
confirm.setOnClickListener(positiveListener);
cancel.setOnClickListener(negativeListener);
return dialog;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值