自定义弹框样式工具类

Posted on 2016-07-29 11:05  lisxu  阅读(126)  评论(0)    收藏  举报

 

public class MyDialog {
private static MyDialog dialog;

public static MyDialog getInstance() {
if (dialog == null) {
dialog = new MyDialog();
}
return dialog;
}

private OnDialogClickListener linstener;

public AlertDialog showAlert(final Context context, String msg) {
if (context instanceof Activity && ((Activity) context).isFinishing()) {
return null;
}
Builder builder = new Builder(context);
builder.setCancelable(false);
builder.setTitle("错误详情").setMessage(msg).setNegativeButton("知道了", null).show();
final AlertDialog alert = builder.create();
return alert;
}


public ProgressDialog getProgressDialog(Context context, String title, String message) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setCanceledOnTouchOutside(false);
if (title != null) {
dialog.setTitle(title);
}
if (message != null) {
dialog.setMessage(message);
}
return dialog;
}

public ProgressDialog setTitleProgressDialog(Context context, String title) {
return getProgressDialog(context, title, null);
}

// public ProgressDialog setMessageProgressDialog(Context context, String message) {
// return getProgressDialog(context, null, message);
// }


public Dialog setMessageProgressDialog(Context context, String message) {
final Dialog dialog = new Dialog(context, R.style.no_title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog, null);
TextView textView = (TextView) dialogView.findViewById(R.id.tv_message);
textView.setText(message);

dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}


public Dialog setMessageProgressVideoDialog(Context context, String message) {
final Dialog dialog = new Dialog(context, R.style.no_title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_video, null);
TextView textView = (TextView) dialogView.findViewById(R.id.tv_message);
textView.setText(message);
Animation anim = AnimationUtils.loadAnimation(context, R.anim.rotate);
LinearInterpolator lir = new LinearInterpolator();
anim.setInterpolator(lir);
dialogView.findViewById(R.id.progress_ico).startAnimation(anim);
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
/**
* 显示 登陆对话框
*
* @param context
* @param title
*/
public Dialog showLoginDialg(final Context context, String title, String left, String right) {

final Dialog dialog = new Dialog(context, R.style.no_title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_login, null);
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(false);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
TextView dialog_left = (TextView) dialog.findViewById(R.id.dialog_left);
TextView dialog_right = (TextView) dialog.findViewById(R.id.dialog_right);
LinearLayout linear_msg = (LinearLayout) dialog.findViewById(R.id.linear_msg);
TextView tv_msg = (TextView) dialog.findViewById(R.id.tv_msg);
dialog_title.setText(title);
dialog_left.setText(left);
dialog_right.setText(right);
dialog_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
linstener.onLeftClickListener(v);
}
});

dialog_right.setOnClickListener(new OnClickListener() { // 不退出
@Override
public void onClick(View v) {
linstener.onRightClickListener(v);
}
});
return dialog;
}


/**
* 显示 对话窗口 2个按钮
* @param context
* @param title
* @param color // title的颜色
* @param leftbacground // 左边的 按钮背景
* @param rightbacground // 右边的按钮背景
*/
public Dialog showDialgTwo(final Context context, String title, String left, String right,
int color,
int bacground,
int leftbacground,
int rightbacground
) {

final Dialog dialog = new Dialog(context, R.style.no_title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_two, null);
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(false);
LinearLayout linear = (LinearLayout)dialog.findViewById(R.id.linear);
if (bacground!=0){
linear.setBackgroundResource(bacground);
}
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
dialog_title.setTextColor(color);
TextView dialog_left = (TextView) dialog.findViewById(R.id.dialog_left);
TextView dialog_right = (TextView) dialog.findViewById(R.id.dialog_right);
LinearLayout linear_msg = (LinearLayout) dialog.findViewById(R.id.linear_msg);
TextView tv_msg = (TextView) dialog.findViewById(R.id.tv_msg);
dialog_title.setText(title);
dialog_left.setText(left);
dialog_left.setBackgroundResource(leftbacground);
dialog_right.setText(right);
dialog_right.setBackgroundResource(rightbacground);
dialog_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
linstener.onLeftClickListener(v);
}
});

dialog_right.setOnClickListener(new OnClickListener() { // 不退出
@Override
public void onClick(View v) {
linstener.onRightClickListener(v);
}
});
return dialog;
}


/**
* 显示 对话窗口 1个按钮
*
* @param context
* @param title
*/
public Dialog showDialgSingle(final Context context, String title) {

final Dialog dialog = new Dialog(context, R.style.no_title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_single, null);
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(false);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
TextView dialog_ok = (TextView) dialog.findViewById(R.id.dialog_ok);

dialog_title.setText(title);
dialog_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
linstener.onLeftClickListener(v);
}
});
return dialog;
}

public void setDialogLinstener(OnDialogClickListener dialog) {
this.linstener = dialog;
}


/***
* 爱车控制 门窗 提交信息
*
* @param context
* @param listMsg
* @return
*/
public Dialog getControlDoorWindowDialog(Context context, List<String> listMsg) {
final Dialog dialog = new Dialog(context, R.style.no_title);

View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_control_door_window, null);
LinearLayout linear_add = (LinearLayout) dialogView.findViewById(R.id.linear_add);
ScrollView scrollView = (ScrollView) dialogView.findViewById(R.id.scrollView);
if (listMsg.size() > 5) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, Tools.dp2px(context, 160));
scrollView.setLayoutParams(params);
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(params);
}
Button btn_cacel = (Button) dialogView.findViewById(R.id.btn_cacel);
for (String msg : listMsg) {
View view = LayoutInflater.from(context).inflate(R.layout.text, null);
TextView tv = (TextView) view.findViewById(R.id.tv_video);
tv.setText(msg);
linear_add.addView(view);
}

btn_cacel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}


public interface OnDialogClickListener {
/**
* 点击左边按钮
*/
public void onLeftClickListener(View v);

/**
* 点击右边按钮
*/
public void onRightClickListener(View v);
}

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3