The use of android-custom bullet frame (alertDialog)

Android's own bullet frame is ugly and troublesome, it is better to define the bullet frame yourself.

1. Come on a button and give a listener event. Trigger a popup when the button is clicked

Insert picture description here
Insert picture description here

Two, come to an AlertDialog.Builder

Insert picture description here

Three, create an alertDialog

Insert picture description here

Fourth, use alertdialog's setView to load a view defined by ourselves (the view code will not be recorded)

Insert picture description here

Five, then submit the view, get our custom control to implement the monitoring method

Insert picture description here

Six, realize the click method, run to see the effect

Insert picture description here

7. Repair, click the blank but the pop-up window will not disappear and click the button to close the pop-up window

Insert picture description here

8. The main code is as follows

//按钮监听事件
    public void sss(View view) {
    
    
        builder = new AlertDialog.Builder(this);
        alertDialog = builder.create();
        View inflate = View.inflate(this, R.layout.dialog, null);
        alertDialog.setView(inflate);
//        點擊空白不關閉彈窗
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
        inflate.findViewById(R.id.btn_yes).setOnClickListener(this);
        inflate.findViewById(R.id.btn_no).setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()){
    
    
            case R.id.btn_yes:
                Toast.makeText(this,"yes",Toast.LENGTH_SHORT).show();
//                關閉彈窗
                alertDialog.dismiss();
                break;
            case R.id.btn_no:
                Toast.makeText(this,"no",Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
                break;
        }
    }

Guess you like

Origin blog.csdn.net/Willow_Spring/article/details/112684393