How to set button click listener for Custom Dialog?

后端 未结 7 425
遇见更好的自我
遇见更好的自我 2021-01-11 11:30

I have made a main Dialog class on which I send the layout ID and shows the layout as a Dialog now when I send the layout from calling class it pop

相关标签:
7条回答
  • 2021-01-11 12:11
    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
    }
    
    0 讨论(0)
  • 2021-01-11 12:13

    There you go:

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(id);
    
        Button signInbutton=(Button)findViewById(R.id.signInButton);
        signInButton.setOnClickListener(this);
        Button closebutton=(Button)findViewById(R.id.closeButton);
        closeButton.setOnClickListener(this);
        }
    
    @Override
    public void onClick(View v) {
        if(R.id.closeButton == v.getId()) { 
            closeButton(v);
        } else {
    
        // do the same for signInButton
        }
    
    }
    

    Suggest you learn the basic beforehand.

    0 讨论(0)
  • 2021-01-11 12:15
     dialog_bring_button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                  final Dialog dialog = new Dialog(MyActivity.this);
                  dialog.setContentView(R.layout.MyCustomDialogxmlfile);
    
                  dialog.show(); 
    
                /* My Cancel Button , and its listener */
    
                  my_cancel_btn=(Button)dialog.findViewById(R.id.datesetbtn);
                  my_cancel_btn.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                         dialog.dismiss();
                    }
                });
    
            }
        });
    
    0 讨论(0)
  • 2021-01-11 12:15

    I find that the code is clearer to seperate my click handlers. Add this in the onCreate method:

    signInbutton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         // do stuff for signInButtonClick
      }
    });
    
    // same for other button
    
    0 讨论(0)
  • 2021-01-11 12:19

    In 2019 this work for me. You can change text view with button.

      Dialog mdialog = new Dialog(this);
          mdialog.setContentView(R.layout.activity_select_type);
            mdialog.show();
    TextView view = mdialog.findViewById(R.id.viewQuotes);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Working...", Toast.LENGTH_SHORT).show();
        }
    });
    
    0 讨论(0)
  • 2021-01-11 12:20
        submit.setOnClickListener(new View.OnClickListener()
               {
    
                @Override
                public void onClick(View v)
                       {                   
             CustomDialog dialog1 = new CustomDialog(Classname.this);
                      dialog1.setContentView(R.layout.submitdialog);
                      dialog1.setTitle(" SUBMIT :");
                    TextView text = (TextView) dialog1.findViewById(R.id.message);
                      text.setText("  Your Answer is correct  ");
                        }
                    }
    
    0 讨论(0)
提交回复
热议问题