Android自定义Dialog对话框

前言

Android项目经常需要使用对话框来进行交互,本文将介绍一个简单自定义的Dialog案例

效果图

在这里插入图片描述

代码部分

自定义Dialog布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyDialog"
android:background="@drawable/dialog_layout">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="提示"
android:textStyle="bold"
android:textSize="24sp"
android:gravity="center"
android:padding="10dp"/>

<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:padding="20dp"
android:gravity="center"
android:text="删除"/>

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#000"
></View>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:padding="10dp"
android:id="@+id/canel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#11c2ee"
android:textSize="24sp"
android:gravity="center"/>

<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000">
</View>

<TextView
android:padding="10dp"
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#11c2ee"
android:layout_weight="1"
android:textSize="24sp"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>

自定义Dialog背景(圆角)

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
//内部填充色
<solid android:color="#ffffff"></solid>
//圆角半径
<corners android:radius="20dp"></corners>
</shape>

通过style样式引用

1
2
3
4
5
6
7
8
9
10
11
12
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>

自定义Dialog类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cn.sr.alertdialogdemo;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class MyDialog extends Dialog implements View.OnClickListener{

private TextView mTitle,mMessage,mCanel,mConfirm;
private String title,message,canel,confirm;
private OnCancelListtener onCancelListtener;
private OnConfirmListtener onConfirmListtener;

public MyDialog setTitle(String title) {
this.title = title;
return this;
}

public MyDialog setMessage(String message) {
this.message = message;
return this;
}

public MyDialog setCanel(String canel,OnCancelListtener onCancelListtener) {
this.canel = canel;
this.onCancelListtener = onCancelListtener;
return this;
}

public MyDialog setConfirm(String confirm,OnConfirmListtener onConfirmListtener) {
this.confirm = confirm;
this.onConfirmListtener = onConfirmListtener;
return this;
}


public MyDialog(@NonNull Context context) {
super(context);
}

public MyDialog(@NonNull Context context,int themeId) {
super(context,themeId);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_dialog);

//如果对话框宽度异常,可以通过下方代码根据设备的宽度来设置弹窗宽度
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams params = getWindow().getAttributes();
Point point=new Point();
display.getSize(point);
params.width= (int) (point.x * 0.8);
getWindow().setAttributes(params);

mTitle=findViewById(R.id.title);
mMessage=findViewById(R.id.message);
mCanel=findViewById(R.id.canel);
mConfirm=findViewById(R.id.confirm);

mCanel.setOnClickListener(this);
mConfirm.setOnClickListener(this);

mTitle.setText(title);
mMessage.setText(message);
mCanel.setText(canel);
mConfirm.setText(confirm);


}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.canel:
if(onCancelListtener != null){
onCancelListtener.onCancel(this);
}
dismiss();
break;

case R.id.confirm:
if(onConfirmListtener != null){
onConfirmListtener.onConfirm(this);
}
dismiss();
break;
}
}
//自定义接口形式提供回调方法
public interface OnCancelListtener{
void onCancel(MyDialog myDialog);
}
public interface OnConfirmListtener{
void onConfirm(MyDialog myDialog);
}
}

调用演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MainActivity extends AppCompatActivity {

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog=new MyDialog(MainActivity.this);
myDialog.setTitle("提示").setMessage("这是一个自定义的Dialog").setCanel("取消", new MyDialog.OnCancelListtener() {
@Override
public void onCancel(MyDialog myDialog) {
Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
}
}).setConfirm("确认", new MyDialog.OnConfirmListtener() {
@Override
public void onConfirm(MyDialog myDialog) {
Toast.makeText(MainActivity.this,"点击了确认按钮",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
}

项目下载

等待上传~


Android自定义Dialog对话框
https://www.srblog.cn/posts/1bd356c0/
作者
sr
发布于
2018年11月19日
许可协议