Screen APIs for Java Mobile Software graphic interface APIs

Source: Internet
Author: User

After reading a screen example, you should ask: How do you organize these screens? How do I navigate between them? How do you manage display and input devices? The answer is that all functions are implemented by the Display class. It provides methods for displaying objects on the screen of a device and searching properties of a device.

  1. Display

A reference pointing to the display of the device can be obtained by providing a MIDlet reference to the static method getDisplay.

Public static Display getDisplay (MIDlet c );
This is typically done in the startApp () method of a MIDlet, as follows:
Public class MyMIDlet extends MIDlet {
Display display = null;
Public MyMIDlet (){}
Public void startApp (){
Display = Display. getDisplay (this );
}
// Other methods
}

Tip: after calling the startApp () method of the MIDlet, call the getDisplay () method, as described above. However, this method should never be called from the MIDlet constructor, as described in each MIDP manual, because the application manager may not complete its initialization at this time.

After obtaining a display reference for a device, you only need to create a GUI component for display. Note that the abstract class Displayable is implemented for all GUI components in Figure 5-2. You can pass the generated GUI component to the two setCurrent () Methods of Display:

public void setCurrent(Displayable d);
public void setCurrent(Alert alert, Displayable d);

The second method is used to display a temporary warning, followed by a GUI element that can be displayed. We will discuss alert later.

To determine the content currently displayed on the device, you can use the getCurrent () method, which returns a reference for the currently displayed Displayable object.

public Displayable getCurrent( );
In addition, the Display class (which is actually a Device Manager) also provides methods for querying the display class to determine the number of colors it supports:

public void boolean inColor( );
public int numColors( );

The first method isColor () returns a Boolean value: true-if the device supports color; false-if only grayscale is supported. The numColors () method returns the integer numbers of different colors supported by the device.

  2. Screen

As mentioned above, the basic unit for interaction between a user and a device is screen, which is an object that encapsulates the user input interface of the device-related graphics. As shown in the class diagram in Figure 1, there are four types of high-level screen: TextBox, List, Alert, and Form.


Figure 1. Class relationship diagram between main classes in the package lcdui
  

However, Screen is only an abstract class with its own functions. Each Screen has two additional features: Title and ticker. The title is a string displayed on top of the screen content. A Ticker is a graphical component that is displayed above the title and can be used to horizontally scroll the information displayed to the user. Both are optional, although the title string has a default content. If ticker is omitted, It is not displayed at all and the corresponding space is given to screen. We will discuss the Ticker component later. However, Figure 2 shows the relative positional relationship between the title of the Screen object and the ticker attribute.


Figure 2. Title and ticker on the screen
  

The following methods of the Screen class can be used to set and retrieve the title and ticker respectively.

public void setTitle(String title);
public String getTitle( );
public void setTicker(Ticker ticker);
public Ticker getTicker( );

  3. Ticker

The Ticker class can display a string of consecutive texts across the display area. It can be attached to one or more previously described screens: Alert, TextBox, List, and Form. You can use the Ticker constructor to create a ticker object:

public Ticker(String str);
You can use the following method to access text strings in ticker:

public String getString( );
public void setString(String s);

Once a ticker is generated, you can use the setTicker () method of the screen to attach it to the screen. For example, the following code generates a List screen and attaches a ticker to it:

Display display = Display.getDisplay( );
List list = new List("Trade Stocks", Choice.EXCLUSIVE);
list.append("Buy", null);
list.append("Sell", null);
list.setTicker(new Ticker("Welcome to my discount broker"));
display.setCurrent(list);

Figure 3 shows the appearance of ticker, which is above the List component currently displayed.


Figure 3. Example of a ticker
  

Pay attention to the following points when using ticker:

· No method function is provided to start or stop the ticker.

· The ticker string is continuously rolled. That is, when the string ends on the display screen, the ticker starts to display at the start position of the string. Adding spaces at the end of the string may make the display of the string appear not to be connected together.

· The rolling direction and speed are determined by the implementation of the MIDP system.

 4. TextBox

A TextBox object is a screen that allows users to input and edit text. When the MIDlet program requires the user to enter such words as name, phone number, e-mail address, or password, you can use the TextBox Control. You can create a TextBox object by specifying four parameters for the TextBox constructor:

public TextBox(String title, String text, int maxSize, int constraints);
Title is reused as the title of the screen. The text and maxSize parameters are used to specify the initial (default) text and the maximum size of the text box. Finally, the constraints parameter can be used to restrict user input. The Constraints value is a static constant integer constant of the TextBox class, which is shared by TextField and TextBox class, as follows:

Constant Description
TextField. ANY Allow users to enter any characters
TextField. EMAILADDR You must enter an email address.
TextField. NUMBER You must enter an integer value.
TextField. PASSWD The text is masked, so the input text is invisible.
TextField. PHONENUMBER You must enter a phone number.
TextField. URL You must enter a URL

If the specified value for constraints is not TextField. ANY, the system will perform a verification to ensure that the input meets the requirements.

Note: The constraint value TextField. PASSWD can be combined with any other constraint value through the OR (|) operator. For example, if you want to generate a TextBox and restrict the input to phone numbers, but want to hide the input data, you can create the following object:

TextBox t=new TextBox("Tel","",12,TextField.PHONENUMBER|TextField.PASSWD);
To retrieve the constraint value of the currently active TextBox, you can:

public int getConstraints( );
public void setConstrants(int c);

It should also be noted that a text box has a capacity limit (that is, the maximum number of characters ). However, each MIDP System Implementation sets a boundary value for the maximum capacity, which may be smaller than the size required by the application. You can use the getMaxSize () method to retrieve the maximum size allowed by the system, or use the setMaxSize () method to reset it.

public int getMaxSize( );
public void setMaxSize(int size);

A well-written MIDP application should always compare the size required by it with the current maximum value.

In the reference implementation of the current MIDP from Sun Microsystems, The getMaxSize () method always returns the size required by the MIDlet. But do not give up the habit of comparison.
You can also set or retrieve the complete text of the TextBox object by using setString () and getString:

public String getString( );
public void setString(String s);

In addition, if you want to know the number of characters entered, you can use the size () method ():

public int size( );
You can also use the following methods to delete, insert, and replace text boxes:

public void delete(int offset, int length);
public void insert(char[] data, int offset, int length, int position);
public void insert(String src, int position);
public void setChars(char[] data, int offset, int length);

Finally, you can use the following method to determine the insert point position:

public int getCaretPosition( );
The following example creates a text box with the label "TextBox". The initial text value is "This is a text box" and the maximum size is 20 characters. You can enter any character.

TextBox tb = new TextBox("TextBox", "This is a textbox", 20, TextField.ANY);
Display display = Display.getDisplay(this);
display.setCurrent(tb);

If you write a complete MIDlet program and run it in the simulator, you will see the effect shown in Figure 4. Note: If the text to be displayed is longer than one screen, the system allows users to scroll to view and edit the text-depending on the system implementation.


Figure 4. Relationship between display and screen <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.