自定义Dialog,弹出提示。总的来说就是 extends Dialog类。
下面列出一些Dialog的基本属性:
1 AlertDialog.Builder属性
* setTitle: 为对话框设置标题 ;
* setIcon : 为对话框设置图标;
* setMessage: 为对话框设置内容;
* setView : 给对话框设置自定义样式 ;
* setItems: 设置对话框要显示的一个list,一般用于显示几个命令时;
* setMultiChoiceItems:用来设置对话框显示一系列的复选框;
* setNeutralButton : 响应中立行为的点击;
* setPositiveButton : 响应Yes/Ok的点击 ;
* setNegativeButton :响应No/Cancel的点击 ;
* create : 创建对话框 ;
* show : 显示对话框;
2 ProgressDialog属性
*setProgressStyle: 设置进度条风格,风格为圆形,旋转的;
*setTitlt: 设置ProgressDialog 标题;
*setMessage: 设置ProgressDialog提示信息;
*setIcon: 设置ProgressDialog标题图标;
*setIndeterminate: 设置ProgressDialog 的进度条是否不明确;
*setCancelable: 设置ProgressDialog 是否可以按返回键取消;
*setButton: 设置ProgressDialog 的一个Button(需要监听Button事件);
*show: 显示ProgressDialog。
如果了解了Dialog的基本属性。我们在自定义一个Dialog。呈现出我们自己的弹窗
public class HintToast extends Dialog {
public HintToast(Context context) {
super(context);
}
public HintToast(Context context, int theme) {
super(context, theme);
}
protected HintToast(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public static class Builder {
private Context mContext;
private View mView;
private String mMessage;
private String positiveButtonText; // 确定按钮
private String negativeButtonText; // 取消
private DialogInterface.OnClickListener positiveButtonClickListener; // 确定监听
private DialogInterface.OnClickListener negativeButtonClickListener;
public Builder(Context context) {
this.mContext = context;
}
public Builder setMessage(String message) {
this.mMessage = message;
return this;
}
public Builder setContentView(View view) {
this.mView = view;
return this;
}
// 设置监听(确定)
public Builder setPositiveButton(String string, DialogInterface.OnClickListener listener) {
this.positiveButtonText = string;
this.positiveButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(String string, DialogInterface.OnClickListener listener) {
this.negativeButtonText = string;
this.negativeButtonClickListener = listener;
return this;
}
public HintToast create() {
LayoutInflater infalter = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = infalter.inflate(R.layout.mimicam_toast, null);
final HintToast dialog = new HintToast(mContext, R.style.Dialog);
// 是ViewGroup 下的LayoutParans 包
LayoutParams layoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
dialog.addContentView(layout, layoutparams);
// z设置确定 按钮
Button ok = (Button) layout.findViewById(R.id.id_toast_ok);
if (null != positiveButtonText) {
ok.setText(positiveButtonText);
if (null != positiveButtonClickListener) {
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
ok.setVisibility(View.GONE);
}
//设置取消 按钮
Button no = (Button) layout.findViewById(R.id.id_toast_no);
if (null != negativeButtonText) {
no.setText(negativeButtonText);
if (null != negativeButtonClickListener) {
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
no.setVisibility(View.GONE);
}
// 设置消息
if (null != mMessage) {
TextView message = (TextView) layout.findViewById(R.id.id_toast_message);
message.setText(mMessage);
} else if (null != mView) {
LinearLayout linear = (LinearLayout) layout.findViewById(R.id.layout_content);
linear.removeAllViews();
linear.addView(mView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
下面是我们自定义的XML 布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/public_dialog_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/id_image_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="52dp"
android:src="@drawable/public_dialog_icon"/>
<TextView
android:id="@+id/id_toast_message"
style="@style/text_16_666666"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="62dp"
android:text="提示内容"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/id_toast_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="120dp"
android:background="@drawable/dialog_button_ok"
android:text="确定"/>
<Button
android:id="@+id/id_toast_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_button_ok"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
引用的背景Drawable xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/public_dialog_ok">
<color android:color="@color/red"></color>
</item>
<item android:state_pressed="false" android:drawable="@drawable/public_dialog_no"></item>
</selector>
简单的两张图片而已。