Setting Up Buttons and Listeners

We now move on to step 5: setting up buttons. You need to provide OK and Cancel buttons so the user can respond to the prompt. If the user clicks Cancel, then the program doesn't need to read any text for the prompt. If the user clicks OK, the program gets the value from the text and passes it back to the activity.

To set up these buttons, you need a listener to respond to these callbacks. We will give you the code for the listener in the "Prompt Dialog Listener" section, but first examine the button setup in Listing 5-24.

Listing 5-24. Setting Up OK and Cancel Buttons //add buttons and listener

PromptListener pl = new PromptListener(view,ctx); builder.setPositiveButton("OK", pl); builder.setNegativeButton("Cancel", pl);

The code in Listing 5-24 assumes that the name of the listener class is PromptListener. We have registered this listener against each button.

0 0

Post a comment