How can I create positive and negative buttons at custom dialogs

前端 未结 2 1035
有刺的猬
有刺的猬 2020-12-19 18:33

I want to create a custom dialog. So i create a template \'dialog_change\' and I open the dialog.

Dialog myDialog = new Dialog(Overview.this);
myDialog.setCo         


        
2条回答
  •  星月不相逢
    2020-12-19 19:08

    You can add the two buttons to the custom layout that you are using for dialog(i.e. dialog_change). And then you can access them after creating the dialog as follows:

    Dialog myDialog = new Dialog(Overview.this);
    View view = LayoutInflater.from(context).inflate(R.layout.dialog_change,null);
    myDialog.setContentView(view);
    myDialog.setTitle("My Custom Dialog Title");
    
    Button button1 = (Button)view.findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v){
            dialog.dismiss();
        }
    });
    //Similarly for the second button
    myDialog.show();
    

提交回复
热议问题