package com.ys100.ysaudio.dialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.ys100.ysaudio.R; public class CheckDialog extends Dialog{ public CheckDialog(Context context) { super(context); } public CheckDialog(Context context, int themeResId) { super(context, themeResId); } protected CheckDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public static class Builder{ private Context context; private String title; private String positiveButtonText; private String negativeButtonText; private OnClickListener positiveButtonClickListener; private OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } /** * Set the Dialog title from resource * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } /** * Set the Dialog title from String * * @param title * @return */ public Builder setTitle(String title) { this.title = title; return this; } /** * Set the positive button resource and it's listener * * @param positiveButtonText * @return */ public Builder setPositiveButton(int positiveButtonText, CheckDialog.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, CheckDialog.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, CheckDialog.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, CheckDialog.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public CheckDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CheckDialog dialog = new CheckDialog(context, R.style.Dialog); View layout = inflater.inflate(R.layout.dialog_clear_message, null); // set the dialog title ((TextView) layout.findViewById(R.id.dialog_clear_message_tv)).setText(title); // set the confirm button if (positiveButtonText != null) { ((TextView) layout.findViewById(R.id.dialog_clear_message_sure)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((TextView) layout.findViewById(R.id.dialog_clear_message_sure)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { dialog.dismiss(); positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.dialog_clear_message_sure).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((TextView) layout.findViewById(R.id.dialog_clear_message_cancel)).setText(negativeButtonText); if (negativeButtonClickListener != null) { ((TextView) layout.findViewById(R.id.dialog_clear_message_cancel)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { dialog.dismiss(); negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.dialog_clear_message_cancel).setVisibility( View.GONE); } dialog.setContentView(layout); return dialog; } } }
转载于:https://my.oschina.net/mengxiaojie/blog/692245