android 對話框的封裝

android對話框工具類的封裝

/**
* 對話框
*/
private static ProgressDialog mProgressDialog;

/**
* 默認的對話框
* 標題、內容、兩個按鈕
* @param context
* @param title
* @param content
* @param btnOKStr 是空字符的話 該按鈕不顯示 (特別註意)
* @param btnCancelStr 是空字符的話 該按鈕不顯示
* @param onClick
*/
public static void showDefaultDialog(Context context,String title,String content,
String btnOKStr,String btnCancelStr,OnClickListener onClick){
showDefaultDialog(context, title, content, btnOKStr, btnCancelStr, onClick, null);
}

/**
* 默認的對話框 + cancel事件
* @param context
* @param title
* @param content
* @param btnOKStr
* @param btnCancelStr
* @param onClick
* @param onCancel
*/
public static void showDefaultDialog(Context context,String title,String content,
String btnOKStr,String btnCancelStr,
OnClickListener onClick,OnCancelListener onCancel){
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
if(title != null){
mBuilder.setTitle(title);
}
mBuilder.setMessage(content);

if(btnOKStr == null)
btnOKStr = “確定”;

if(btnCancelStr == null)
btnCancelStr = “取消”;

if(!btnOKStr.trim().equals(“”))
mBuilder.setPositiveButton(btnOKStr, onClick);

if(!btnCancelStr.trim().equals(“”))
mBuilder.setNegativeButton(btnCancelStr, onClick);
if(onCancel != null){
mBuilder.setOnCancelListener(onCancel);
}
mBuilder.show();
}

public static void showProgressDialog(Context context,String msg,boolean cancelable) {
// dismissProgressDialog();
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage(msg);
mProgressDialog.setCancelable(cancelable);
mProgressDialog.show();
}

public static void dismissProgressDialog(){
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}

發佈留言