由于项目的需要,系统的弹出框已经不能满足我们的需求,我们需要各式各样的弹出框,这时就需要我们去自定义弹出框了。
新建布局文件 dialog_layout.xml,将下面内容复制进去
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape_linearlayout"
android:orientation="vertical"
>
android:layout_height="150dp"
android:gravity="center_vertical"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="TextView" />
android:layout_height="1dp"
android:background="@color/line" />
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="1dp" >
android:layout_width="1dp"
android:layout_height="40dp"
android:background="@color/line" />
新建java类DialogUtil.java
public class DialogUtil {
private static DialogUtil dialogUtil;
private Dialog dialog;
private TextView message;
private Button firstBtn;
private Button secondBtn;
private onButtnClickListener buttnClickListener;
public static DialogUtil getDialogInstance(Context context) {
if (null == dialogUtil) {
dialogUtil = new DialogUtil(context);
}
return dialogUtil;
}
private DialogUtil(Context context) {
initDialog(context);
}
public void setMessage(String message) {
if (null != message && !message.equals("")) {
this.message.setText(message);
}
}
private void initDialog(Context context) {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setCanceledOnTouchOutside(false);
Window window = dialog.getWindow();
dialog.setCanceledOnTouchOutside(true);
WindowManager.LayoutParams params = window.getAttributes();
params.width = ScreenUtils.getScreenWidth(context)
- ScreenUtils.getScreenWidth(context) / 4;
params.height = LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
window.setAttributes(params);
window.setWindowAnimations(R.style.displayStyle);
message = (TextView) dialog.findViewById(R.id.txt_dialog_message);
firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_go);
secondBtn = (Button) dialog.findViewById(R.id.btn_dialog_add);
firstBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (null != buttnClickListener)
buttnClickListener.onFirstBtnClick(v);
}
});
secondBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (null != buttnClickListener)
buttnClickListener.onSecondBtnClick(v);
}
});
}
public void showDialog() {
if (dialog != null && !dialog.isShowing()) {
dialog.show();
}
}
public void dissmissDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
this.buttnClickListener = buttnClickListener;
}
public interface onButtnClickListener {
public void onFirstBtnClick(View v);
public void onSecondBtnClick(View v);
}
}
dialog工具类采用的是单列的模式,调用的流程是
1、先获取对象
DialogUtil.getDialogInstance(context);
2、设置文本信息
setMessage(String message);
3、设置按钮的单机事件
setOnButtnClickListener(onButtnClickListener buttnClickListener);
4、显示dialog
showDialog();
至此自定义弹出框就完成了