Android AlertDialog always exits when I click on OK Button

后端 未结 2 1300
北恋
北恋 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: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
            }
    
        }
    });
    

提交回复
热议问题