Handling Button Clicks

Handling the click event of a Button control is achieved by using the setOnClickListener() method. Specifically, you need to implement the onClick() method of View.OnClickListener. This is where any event handling for clicks should take place.

For example, to handle when a user clicks the Button control called Button_DOB, you add the following code to the onCreate() method of the QuizSettingsActivity class to handle the event:

Button pickDate = (Button) findViewById(R.id.Button_DOB); pickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

// Handle date picking dialog

You retrieve the Button control by using the findViewById() method, and then you set View.OnClickListener for the control by using the setOnClickListener() method. It is within the onClick() method that you want to trigger DatePickerDialog to be launched. However, you are not quite ready to implement this Dialog window yet. Instead, you can drop in a useful little helper pop-up message called a toast.

0 0

Post a comment