自定义dialog

package cn.juzhong.view.widget;

import cn.juzhong.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author wanggenping
 *
 */
public class CustomDialog extends Dialog{
    /**
     * @param context
     * @param theme
     */
    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }
    
    public static class Builder{
        private Context context;
        private boolean isCancel;
        private CustomDialog dialog;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private TextView titleTextView;
        private TextView contentTextView;
        private Button positiveButton;
        private Button negativeButton;
        private View line;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;
        
        public Builder(Context context, boolean isCancel){
            this.context = context;
            this.isCancel = isCancel;
        }
        
        public Builder setMessage(String message) {  
            this.message = message;  
            return this;  
        } 
        
        public Builder setMessage(int message) {  
            this.message = (String) context.getText(message);  
            return this;  
        } 
        
        public Builder setTitle(String title) {  
            this.title = title;  
            return this;  
        } 
        public String setTitle(int title) {  
            this.title = (String) context.getText(title);  
            return this.title;  
        } 
        
        public Builder setContentView(View v) {  
            this.contentView = v;  
            return this;  
        } 
        
        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;  
        }
        
        public CustomDialog create() {  
            LayoutInflater inflater = (LayoutInflater) context  
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new CustomDialog(context,R.style.Dialog);  
            contentView = inflater.inflate(R.layout.view_custom_dialog, null);
            titleTextView = (TextView) contentView
                    .findViewById(R.id.view_custom_dialog_title_textview);
            contentTextView = (TextView) contentView
                    .findViewById(R.id.view_custom_dialog_content_textview);
            line = contentView
                    .findViewById(R.id.view_custom_dialog_vertical_line);
            positiveButton = (Button) contentView
                    .findViewById(R.id.view_custom_dialog_positive_btn);
            negativeButton = (Button) contentView
                    .findViewById(R.id.view_custom_dialog_negative_btn);
            if(!TextUtils.isEmpty(positiveButtonText)){
                
                positiveButton.setText(positiveButtonText);
                if(positiveButtonClickListener != null){
                    positiveButton.setOnClickListener(new View.OnClickListener() {
                        
                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            positiveButtonClickListener.onClick(dialog,  
                                    DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
                
            }else{
                positiveButton.setVisibility(View.GONE);
                line.setVisibility(View.GONE);
            }
            
            if(!TextUtils.isEmpty(negativeButtonText)){
                
                negativeButton.setText(negativeButtonText);
                if(negativeButtonClickListener != null){
                    negativeButton.setOnClickListener(new View.OnClickListener() {
                        
                        @Override
                        public void onClick(View arg0) {
                            negativeButtonClickListener.onClick(dialog,  
                                    DialogInterface.BUTTON_NEGATIVE); 
                        }
                    });
                }
                
            }else{
                negativeButton.setVisibility(View.GONE);
                line.setVisibility(View.GONE);
            }
            
            if(!TextUtils.isEmpty(title)){
                titleTextView.setText(title);
            }
            if(!TextUtils.isEmpty(message)){
                contentTextView.setText(message);
            }
            dialog.setCancelable(isCancel);
//            dialog.addContentView(contentTextView, new LayoutParams(  
//                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.setContentView(contentView);
            return dialog;  
        }
        
        public void show(){
            if(dialog != null){
                dialog.show();
            }
        }
        
        public void dismissDialog(){
            if(dialog != null){
                dialog.dismiss();
            }
        }
    }
        
    }

    //界面中应用

CustomDialog.Builder builder = new CustomDialog.Builder(fragmentActivity, false);
builder.setTitle(R.string.dialog_check_update_title_text);
            builder.setMessage(R.string.dialog_check_update_message_version_check_text);
            builder.setPositiveButton(R.string.dialog_check_update_cancel_text, new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    arg0.dismiss();
                }
            });
            builder.create().show();


你可能感兴趣的:(android,dialog)