I am using AlertDialog.Builder to create a dialog with a EditText and two buttons, 'OK' and 'Cancel'. I create the OK and Cancel buttons with AlertDialog.Builder.setPositiveButton() and .setNegativeButton() respectively. The purpose of the dialog is to request an I.P. address from the user.
Initially I want to have the OK button disabled and attach to the EditText an OnKey listener so that when the user types the onKey listener is called and I can check the current EditText value with a regular expression for a valid IP address. Should a valid IP address be entered I’d then want to enable the OK button, but as I use the setPositiveButton() I have no idea what the id of the button is.
Can I get the id of the OK button?
You dont need the id of the button,all you need is to implement
DialogInterface.onClickListener()In the onKeyListener() you add some test if the field is filled with correct input or not
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(enabled);And for the click listener of the OK button. Remember that you need to set the button before you do
.getButton()