Android AlertDialog always exits when I click on OK Button

后端 未结 2 1285
北恋
北恋 2020-12-30 17:48

Is there some way to do validation upon clicking ok. Im not dismissing it but it closes when I click Ok. I have written a custom AlertDialog that uses some edit texts. Probl

相关标签:
2条回答
  • 2020-12-30 18:39

    if you write nothing inside onClick method definately it will dismiss whether you want or not, then why you asking this question as you have not write anything inside that method. as It is alertdialog it will get closed and if you dont want it close then use setNeutralButton instead of setPositiveButton. It wont close if you use neutral button

    0 讨论(0)
  • 2020-12-30 18:49

    You can do this by overriding the onClickListener. The trick is to get the button after create and showing the dialog.

    //  Create you dialog here and show it
    ...
    dialog.show();
    
    Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View onClick) {
            //  Valid checking
            ...
    
            if (valid) {
                dialog.dismiss();
            } else {
                //  Not valid
            }
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题