Tuesday, December 14, 2010

Getting Input from a Custom Dialog


Creative Commons Licence

This work is licenced under a Creative Commons Licence.

Quite often we want to be able to get some information from the user but don't want to use the MainScreen interface. Instead we would rather use a Dialog. The built in Dialog API has some nice static functions that suspend the program execution until the user responds, then allows execution to continue. They handle all the tedious issues with the event thread for us. Wouldn't it be nice to have a dialog that we could call to get some text, a phone number, or some other piece of information from the user? Well here is how you can do that:

/*
* BasicTextDialog.java
*
* © Richard Buckley www.hrbuckley.net, 2010
*
* This work is licenced under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* To view a copy of this licence, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
* Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
*/

package src.dialog;

import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.DialogClosedListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.system.Bitmap;

//
//  Create a dialog with a BasicEditField to accept user input. This example sets the field style to
//  allow only uppercase input.
//
//
public class BasicTextDialog extends Dialog implements DialogClosedListener {
 private BasicEditField      _text;
 private int                 _choice;

 public BasicTextDialog()  {
  super(Dialog.D_OK_CANCEL, "Enter Some Text.", Dialog.D_OK, Bitmap.getPredefinedBitmap(Bitmap.QUESTION), 0L);
  _text = new BasicEditField("Only Uppercase: ", null, BasicEditField.DEFAULT_MAXCHARS, BasicEditField.FILTER_UPPERCASE);
  add(_text);
  setDialogClosedListener(this);
 }

 //
 //   The dialogClosed listener is called when the user closes the dialog, we can get the selection made at this point.
 //
 public void dialogClosed(Dialog dialog, int choice) {
  _choice = choice;
 }

 //
 //   A static convenience function that takes care of creating the dialog, posting it as a modal window (this blocks
 //   our execution and gives the event thread back to the OS so the user can interact with our dialog).
 //
 public static String askForText() {
  BasicTextDialog nsd = new BasicTextDialog();
  nsd.doModal();

  //
  //    If the user selected OK return the content of the BasicEditField, otherwise return null
  //
  if (nsd._choice == Dialog.OK) {
   return nsd._text.getText();
  } else {
   return null;
  }
 }

 //
 //    This function move the focus to the BasicEditField when the dialog is attached to the UiEngine. If you don't
 //    do this the focus will go to the default button.
 //
 protected void onUiEngineAttached(boolean attached) {
  if (attached) {
   _text.setFocus();
  }
 }
}

Using the new dialog is very easy:

String sometext = BasicEditDialog.askForText();

2 comments:

  1. Great article I've just added to my bookmark list.

    ReplyDelete
  2. Hey this post is nice and interesting. Can you comment me any related articles?

    ReplyDelete