自定义 Android 对话框

在这用到了支持点有:方法传递、参数传递


activity_custom_dialog.xml 文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout> xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提示"
        android:textSize="25dp"
        android:textColor="#000"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你确认删除吗?"
        android:textSize="18dp"
        android:layout_marginTop="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp">
        <Button
            android:id="@+id/qd"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="确定"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/qx"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="取消"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>
CustomDialog.java文件代码
package com.example.testapp;

import androidx.annotation.NonNull;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

interface IMethod {
    void method();
}

public class CustomDialog extends Dialog {

    private String msg;
    private TextView tVmsg;
    private IMethod iMethod;
    private Button btn;
    private Button btn2;

    public CustomDialog(@NonNull Context context, String msg) {
        super(context);
        this.msg = msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_dialog);
        setWidth();
        btn = findViewById(R.id.qd);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iMethod != null) {
                    iMethod.method();
                }
                dismiss();
            }
        });

        btn2 = findViewById(R.id.qx);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tVmsg=findViewById(R.id.tv);
        if (!TextUtils.isEmpty(msg)) {
            tVmsg.setText(msg);
        }
    }

    //设置对话框的宽度
    private void setWidth() {
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) (size.x * 0.8);//相对于手机屏幕的80%
        getWindow().setAttributes(p);
    }

    public void setMethod(IMethod method) {
        iMethod = method;
    }
}


调用对话框
CustomDialog customDialog=new CustomDialog(DialogActivity.this,"你好,确定删除吗?");
customDialog.setMethod(new IMethod() {
    @Override
    public void method() {
        //处理一些逻辑事务代码
        Toast.makeText(DialogActivity.this,"方法传递",Toast.LENGTH_LONG).show();
    }
});
customDialog.show();

评论