How do I display an alert dialog on Android?

后端 未结 30 3113
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

30条回答
  •  暖寄归人
    2020-11-22 03:19

    I was using this AlertDialog in button onClick method:

    button.setOnClickListener(v -> {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
        View view2 = layoutInflaterAndroid.inflate(R.layout.cancel_dialog, null);
        builder.setView(view2);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
    
        view2.findViewById(R.id.yesButton).setOnClickListener(v1 -> onBackPressed());
        view2.findViewById(R.id.nobutton).setOnClickListener(v12 -> alertDialog.dismiss());
    });
    

    dialog.xml

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

提交回复
热议问题