先看下效果图
系统弹框:

自定义弹框:

下面分别对这两种弹框进行说明:
系统弹框代码,也没什么好说的,正式项目中基本用不到,因为太丑了。
/**
* @desc : 系统弹框
* @author : congge on 2021-09-09 10:50
**/
public void systemDialogClick(View view){
new AlertDialog.Builder(mContext)
.setTitle("提示")
.setMessage("确定要删除嘛?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext,"你点击了取消",Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext,"你点击了确定",Toast.LENGTH_LONG).show();
}
})
.create()
.show();
}
我们重点来看下自定义弹框
步骤一:定义弹框的style
<style name="mydialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--是否透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item><!--模糊-->
<item name="android:backgroundDimAmount">0.6</item>
</style>
看不懂没关系,你就当固定的写法
步骤二:设置弹框的View,通过设置dialog的window的setContentView
当然要先建layout
dialog_ok_cancle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="280dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@drawable/dialog_window_bg"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="#333333"
android:text="提示"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#ededed"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_message"
android:textSize="14sp"
android:textColor="#555555"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_cancel"
android:layout_width="140dp"
android:layout_height="40dp"
android:background="@drawable/dialog_window_cancel_bg"
android:gravity="center"
android:text="@string/cancel"
android:textColor="@color/c_main"
android:textSize="16sp" />
<Button
android:id="@+id/btn_ok"
android:layout_width="140dp"
android:layout_height="40dp"
android:background="@drawable/dialog_window_ok_bg"
android:gravity="center"
android:text="@string/ok"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
调用代码:
/**
* @desc : 自定义弹框
* @author : congge on 2021-09-09 11:01
**/
public void customDialogClick(View view){
okAndCancelDialog(mContext, "提示", "确定删除嘛?", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"你点击了确定",Toast.LENGTH_LONG).show();
}
});
}
private void okAndCancelDialog(Context context, String title, String message,
final View.OnClickListener okListener) {
final android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(context, R.style.mydialog).setCancelable(true).create();
dialog.show();
Window window = dialog.getWindow();
window.setContentView(R.layout.dialog_ok_cancel);
((TextView) window.findViewById(R.id.tv_title)).setText(title);
((TextView) window.findViewById(R.id.tv_message)).setText(message);
window.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
window.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
okListener.onClick(v);
}
});
}
网友评论