前言
Android项目经常需要使用对话框来进行交互,本文将介绍一个简单自定义的Dialog案例
效果图
代码部分
自定义Dialog布局
代码语言:javascript复制<?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背景(圆角)
代码语言:javascript复制<?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样式引用
代码语言:javascript复制<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类
代码语言:javascript复制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);
}
}
调用演示
代码语言:javascript复制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();
}
});
}
}
项目下载
等待上传~