在对话框中添加取消按钮

时间:2017-09-05 14:17:26

标签: java android

我有onclick按钮的对话框,并包含final Dialog dialog = new Dialog(context);如何将取消按钮添加到我的对话框以进行隐藏对话框 我的内容布局与活动主要布局不同 对不起我的语言

我的代码:

public void showd(View view){

        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        }

1 个答案:

答案 0 :(得分:1)

您应该使用DialogBu​​ilder而不是自己创建Dialog。然后,您可以使用以下代码

创建取消按钮
// Create your dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Apply your custom view
builder.setView(R.layout.custom);
// A null clickListener will cancel the dialog
builder.setNegativeButton("Cancel" /*TODO Replace with string resources*/, null);
// Show the dialog
builder.show();