使用方法 example:
DialogUtil.showAlertDialog(getActivity(), R.mipmap.restart, "退出提示", "你确定要退出吗?"),
"确定", "取消", true, new DialogUtil.AlertDialogBtnClickListener() {
@Override
public void clickPositive() {
//positive
}
@Override
public void clickNegative() {
//negative
}
});
工具类主要代码
public class DialogUtil {
private static AlertDialog dialog;
/**
* @param activity Context
* @param iconRes 提示图标
* @param title 提示标题
* @param msg 提示内容
* @param positiveText 确认
* @param negativeText 取消
* @param cancelableTouchOut 点击外部是否隐藏提示框
* @param alertDialogBtnClickListener 点击监听
*/
public static void showAlertDialog(Activity activity, int iconRes, String title, String msg,
String positiveText, String negativeText, boolean
cancelableTouchOut, final AlertDialogBtnClickListener
alertDialogBtnClickListener) {
View view = LayoutInflater.from(activity).inflate(R.layout.custom_dialog_layout, null);
ImageView mIcon = view.findViewById(R.id.icon);
TextView mTitle = view.findViewById(R.id.title);
TextView mMessage = view.findViewById(R.id.message);
Button positiveButton = view.findViewById(R.id.positiveButton);
Button negativeButton = view.findViewById(R.id.negativeButton);
mIcon.setImageResource(iconRes);
mTitle.setText(title);
mMessage.setText(msg);
positiveButton.setText(positiveText);
negativeButton.setText(negativeText);
positiveButton.setOnClickListener(v -> {
alertDialogBtnClickListener.clickPositive();
dialog.dismiss();
});
negativeButton.setOnClickListener(v -> {
alertDialogBtnClickListener.clickNegative();
dialog.dismiss();
});
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setView(view);
builder.setCancelable(true); //返回键dismiss
//创建对话框
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉圆角背景背后的棱角
dialog.setCanceledOnTouchOutside(cancelableTouchOut); //失去焦点dismiss
dialog.show();
}
public interface AlertDialogBtnClickListener {
void clickPositive();
void clickNegative();
}
}
下面是布局文件 custom_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="280dp"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_dialog_title"
android:gravity="center"
android:minWidth="210dp"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp">
<ImageView
android:id="@+id/icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:src="@mipmap/icon"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:layout_marginStart="8dip"
android:text="@string/offlineNotifaction"
android:textColor="@color/colorLightBlack"
android:textSize="16sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_dialog_content"
android:gravity="center"
android:minHeight="80dip"
android:minWidth="210dp"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginStart="10dip"
android:clickable="true"
android:focusable="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textSize="16sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_dialog_button"
android:minWidth="210dp"
android:orientation="horizontal"
android:padding="12dp">
<Button
android:id="@+id/positiveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:background="@drawable/custom_dialog_negative"
android:text="@string/sure"
android:textAllCaps="false"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/negativeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_weight="1"
android:background="@drawable/custom_dialog_positive"
android:text="@string/cancel"
android:textAllCaps="false"
android:textColor="@android:color/white"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
提示框上部分背景 custom_dialog_title
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f5f5f5"/>
<stroke android:width=".7dp" android:color="#c0c0c0"/>
<corners
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
提示框中部分背景 custom_dialog_content
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#f5f5f5"/>
<stroke
android:width=".4dp"
android:color="#848484"/>
</shape>
提示框下部分背景 custom_dialog_button
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f5f5f5"/>
<stroke android:color="#c0c0c0" android:width=".7dp"/>
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"/>
</shape>
确定按钮样式 custom_dialog_negative
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#FFE28A42"/>
<stroke android:width="1dp" android:color="#FFE28A42"/>
<corners android:radius="6dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="#FF9742"/>
<stroke android:width="1dp" android:color="#FF9742"/>
<corners android:radius="6dp"/>
</shape>
</item>
</selector>
取消按钮样式 custom_dialog_positive
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#8F000000"/>
<corners android:radius="6dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="#848484"/>
<corners android:radius="6dp"/>
</shape>
</item>
</selector>