对话框帮助类(二)

主要代码:

public class AlertDialog {
    /** 上下文参数 */
    private Context mContext;
    /** 标题旁边显示图标的ImageView */
    private ImageView mIconIV;
    /** 显示标题的TextView */
    private TextView mTitleTV;
    /** 显示内容的TextView */
    private TextView mContentTV;
    /** 默认显示内容的布局LinearLayout */
    private LinearLayout mDefaultLL;
    /** 显示自定义布局的FrameLayout */
    private FrameLayout mCustomFL;
    /** 最左边的按钮Button */
    private Button mLeftBtn;
    /** 中间的按钮Button */
    private Button mMiddleBtn;
    /** 最右边的按钮Button */
    private Button mRightBtn;
    /** 全局的对话框变量 */
    private android.app.AlertDialog mDialog;
    
    public AlertDialog(Context context){
        mContext = context;
        mDialog = new android.app.AlertDialog.Builder(mContext).create();
        mDialog.setView(View.inflate(context, R.layout.alert_dialog_new, null));
        mDialog.show();
        //替换整个对话框的布局
        Window window = mDialog.getWindow();
        window.setContentView(R.layout.alert_dialog_new);
        
        //获取自定义布局的控件
        mIconIV = (ImageView)window.findViewById(R.id.alert_dialog_icon_iv);
        mTitleTV = (TextView)window.findViewById(R.id.alert_dialog_title_tv);
        mDefaultLL = (LinearLayout)window.findViewById(R.id.alert_dialog_content_ll);
        mContentTV = (TextView)window.findViewById(R.id.alert_dialog_content_tv);
        mCustomFL = (FrameLayout)window.findViewById(R.id.alert_dialog_custom_fl);
        mLeftBtn = (Button)window.findViewById(R.id.alert_dialog_left_btn);
        mMiddleBtn = (Button)window.findViewById(R.id.alert_dialog_middle_btn);
        mRightBtn = (Button)window.findViewById(R.id.alert_dialog_right_btn);
    }
    /**
     * setIcon:设置标题左边图标的资源ID. 
* @author wchhuangya * @param resId - 图标的图片资源ID */ public AlertDialog setIcon(int resId){ mIconIV.setBackgroundResource(resId); return this; } /** * setIcon:设置标题左边图标的资源ID.
* @author wchhuangya * @param title - 标题 */ public AlertDialog setTitle(String title){ mTitleTV.setText(title); return this; } /** * setIcon:设置标题.
* @author wchhuangya * @param msg - 内容 */ public AlertDialog setMsg(String msg){ mContentTV.setText(msg); return this; } /** * setCustomView:设置自定义布局.
* @author wchhuangya * @param resId - 自定义布局的ID */ public AlertDialog setCustomView(int resId){ View view = View.inflate(mContext, resId, null); if(mCustomFL.getChildCount() > 0) mCustomFL.removeAllViews(); mDefaultLL.setVisibility(View.GONE); mCustomFL.addView(view); return this; } /** * getSingleView:(获取自定义布局里的某一控件).
* @author wuhr * @param resId * @return */ public View getSingleView(int resId){ if(mCustomFL.getChildCount() > 0){ return mCustomFL.findViewById(resId); } else { return null; } } /** * setLeftBtnText:设置左边按钮的文字.
* @author wchhuangya - 按钮的文字 */ public AlertDialog setLeftBtnText(String text){ mLeftBtn.setVisibility(View.VISIBLE); mLeftBtn.setText(text); return this; } /** * setLeftBtnText:设置中间按钮的文字.
* @author wchhuangya - 按钮的文字 */ public AlertDialog setMiddleBtnText(String text){ mMiddleBtn.setVisibility(View.VISIBLE); mMiddleBtn.setText(text); return this; } /** * setLeftBtnText:设置右边按钮的文字.
* @author wchhuangya - 按钮的文字 */ public AlertDialog setRightBtnText(String text){ mRightBtn.setVisibility(View.VISIBLE); mRightBtn.setText(text); return this; } /** * setLeftBtnListener:设置左边按钮的事件监听.
* @author wchhuangya * @param listener - 事件监听 */ public AlertDialog setLeftBtnListener(final View.OnClickListener listener){ mLeftBtn.setOnClickListener(listener); return this; } /** * setLeftBtnListener:设置中间按钮的事件监听.
* @author wchhuangya * @param listener - 事件监听 */ public AlertDialog setMiddleBtnListener(final View.OnClickListener listener){ mMiddleBtn.setOnClickListener(listener); return this; } /** * setLeftBtnListener:设置右边按钮的事件监听.
* @author wchhuangya * @param listener - 事件监听 */ public AlertDialog setRightBtnListener(final View.OnClickListener listener){ mRightBtn.setOnClickListener(listener); return this; } /** * isCancelable:是否可以取消对话框.
* @author wchhuangya * @param isCancelable - true:可以取消;false:不能取消; * @return */ public AlertDialog isCancelable(boolean isCancelable){ mDialog.setCancelable(isCancelable); return this; } /** * isCancelable:点击对话框外的区域是否可以取消对话框.
* @author wchhuangya * @param isCancelableOnTouchOutside - true:可以取消;false:不能取消; * @return */ public AlertDialog isCancelableOnTouchOutside(boolean isCancelableOnTouchOutside){ mDialog.setCanceledOnTouchOutside(isCancelableOnTouchOutside); return this; } /** * dismiss:让对话框消失的方法.
* @author wchhuangya */ public void dismiss(){ mDialog.dismiss(); } }

配置文件:

 
    
    
    

demo请移步guthub,传送门:https://github.com/liuxinggen/AlertDialogHelper

你可能感兴趣的:(对话框帮助类(二))