Handling buttons in custom dialogs

后端 未结 1 1969
花落未央
花落未央 2020-12-04 02:50

I have a little problem with an android custom dialog.

I construct a custom dialog in the onCreateDialog(int) function:

dialog = new Dialog(this);
di         


        
相关标签:
1条回答
  • 2020-12-04 03:46

    The way round it that I use, rather than having a switch block is to use onClickListeners for the buttons:

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Custom Dialog");
    
    
    Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
    dialog_btn.setOnClickListener(new View.OnClickListener() 
    {
        // Perform button logic
    }
    

    Note that you are finding the view from the dialog, not just calling straight to findViewById as that would return a null pointer as there would be not dialog_button on the application view.

    0 讨论(0)
提交回复
热议问题