public class NormalDialog extends Dialog {
public NormalDialog(Context context) {
super(context);
}
public NormalDialog(Context context, int theme) {
super(context, theme);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
}
return super.onKeyDown(keyCode, event);
}
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;
private EditText editText;
public String getInputString() {
if (editText == null) {
return null;
}
return editText.getText().toString();
}
public void setInputString(String string) {
this.editText.setText(string);
}
public Builder(Context context) {
this.context = context;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* 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;
}
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @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;
}
public NormalDialog createNormol() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final NormalDialog dialog = new NormalDialog(context, R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_normal, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
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 {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
}
// set the cancel button
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 {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else {
// if no message set
// add the contentView to the dialog body
// ((LinearLayout)
// layout.findViewById(R.id.message)).removeAllViews();
// ((LinearLayout)
// layout.findViewById(R.id.message)).addView(contentView, new
// LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((TextView) layout.findViewById(R.id.message)).setVisibility(View.GONE);
}
dialog.setContentView(layout);
return dialog;
}
}
}
dialog_normal.xml
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical"
android:padding="20.0dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/main_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:textColor="@color/black"
android:textSize="@dimen/normal_title_size" />
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="@color/black"
android:textSize="@dimen/normal_title_size" />
<View
android:layout_width="fill_parent"
android:layout_height="1.0dp"
android:background="@color/dialog_btn_divider" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
android:orientation="horizontal" >
<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/gray_btn_selector"
android:text="@string/cancel"
android:textColor="@color/white_blue_selector" />
<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/blue_bg_btn_selector"
android:text="@string/ok"
android:textColor="@color/white_gray_selector" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
使用方法
<span style="font-family: Arial, Helvetica, sans-serif;">
/**
* 可以创建一个通用的对话框,包含alert\含有message\不含message\
*
* @param title
* @param msg
* @param okListener
* @param cancelListener
*/
public void showNormalDialog(String title, String msg, DialogInterface.OnClickListener okListener,
DialogInterface.OnClickListener cancelListener) {
NormalDialog normalDialog;
NormalDialog.Builder builder = new NormalDialog.Builder(getActivity());
if (msg != null && !msg.equals("")) {
builder.setMessage(msg);
}
builder.setTitle(title);
if (okListener != null) {
builder.setPositiveButton("确定", okListener);
}
if (cancelListener != null) {
builder.setNegativeButton("取消", cancelListener);
}
normalDialog = builder.createNormol();
normalDialog.setCancelable(false);
normalDialog.show();
}</span>