925
技術社區[雲棲]
Android自定義dialog
很多時候,我們需要自己去定義dialog,目前我們就遇見了這樣一個需求,我的想法是自己定義一個dialog,如果有list的話就使用listview,如果有msg的話就使用msg,並且取消和確定按鈕也可自己定義。自定義一個dialog,好處是我們自己定義背景,自己定義事件,自己定義按鈕,能很完美的達到自己想要的效果。
public class CustomDialog extends Dialog {
//實現默認構造函數
public CustomDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
protected CustomDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//所有的方法執行完都會返回一個Builder使得後麵可以直接create和show
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;//確定按鈕
private String negativeButtonText;//取消按鈕
private View contentView;
private BaseAdapter adapter;//listview的adapter
//確定按鈕事件
private DialogInterface.OnClickListener positiveButtonClickListener;
//取消按鈕事件
private DialogInterface.OnClickListener negativeButtonClickListener;
//listview的item點擊事件
private AdapterView.OnItemClickListener listViewOnclickListener;
public Builder(Context context) {
this.context = context;
}
//設置消息
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
*設置內容
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* 設置標題
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
*設置標題
*
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
//設置適配器
public Builder setAdapter(BaseAdapter adapter) {
this.adapter = adapter;
return this;
}
//設置點擊事件
public Builder setOnClickListener(AdapterView.OnItemClickListener listViewOnclickListener) {
this.listViewOnclickListener = listViewOnclickListener;
return this;
}
//設置整個背景
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* 設置確定按鈕和其點擊事件
*
* @param positiveButtonText
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
//設置取消按鈕和其事件
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
//createview方法
public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 設置其風格
final CustomDialog dialog = new CustomDialog(context,R.style.CustomProgressDialog);
View layout = inflater.inflate(R.layout.dialog_custom_layout, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// 設置標題
((TextView) layout.findViewById(R.id.title)).setText(title);
//設置listview的adapter如果沒有就隱藏listview
if(adapter != null && adapter.getCount()>0){
ListView listView = (ListView)layout.findViewById(R.id.listView);
listView.setAdapter(adapter);
if(listViewOnclickListener!=null){
listView.setOnItemClickListener(listViewOnclickListener);
}
}else{
layout.findViewById(R.id.listView).setVisibility(
View.GONE);
}
//設置確定按鈕
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// 如果沒有確定按鈕就將其隱藏
layout.findViewById(R.id.positiveButton).setVisibility(
View.GONE);
}
// 設置取消按鈕
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// 如果沒有取消按鈕就將其隱藏
layout.findViewById(R.id.negativeButton).setVisibility(
View.GONE);
}
// 設置內容
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// 添加view
((LinearLayout) layout.findViewById(R.id.message))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.message)).addView(
contentView, new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
最後更新:2017-04-03 12:53:57