简单创建Dialog

1. 首先新建一个类,继承 Dialog

public Dialog(@NonNull Context context) {
        super(context);
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.mdialoglayout, null);     //通过LayoutInflater创建布局
        textView1 = view.findViewById(R.id.cancle);     //获取取消按钮
        textView2 = view.findViewById(R.id.cancle1);    //获取确认退出按钮
        setContentView(view);               //设置显示的视图
    }
    public void setOnCancelListener(View.OnClickListener listener){
        textView1.setOnClickListener(listener);
    }
    public void setOnCancelListener1(View.OnClickListener listener){
        textView2.setOnClickListener(listener);
    }

2. 在 MainAcitiviy 中处理点击事件

public void onExitNumber(View view){
        final Dialog mDialog = new Dialog(this);
        mDialog.setOnCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                Log.d("MainActivity", "onClick: " + "dismiss()");
            }
        });
        mDialog.setOnCancelListener1(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                finish();
                Log.d("MainActivity", "onClick: " + "finish()");
            }
        });
        mDialog.show();
    }

你可能感兴趣的:(简单创建Dialog)