24/05/2018, 20:02

Thành phần List, Textbox, Timer

Trong phần này chúng ta sẽ xem xét các đối tượng ListBox, TextBox, Alert, và Ticker trong các thành phần giao diện cấp cao của ứng dụng MIDP. Chúng ta hãy cũng xem lại cây phân cấp các thành phần trình bày trên thiết bị hoàn chỉnh hơn ...

Trong phần này chúng ta sẽ xem xét các đối tượng ListBox, TextBox, Alert, và Ticker trong các thành phần giao diện cấp cao của ứng dụng MIDP. Chúng ta hãy cũng xem lại cây phân cấp các thành phần trình bày trên thiết bị hoàn chỉnh hơn

Một List chứa một dãy các lựa chọn được thể hiện một trong ba dạng. Chúng ta đã thấy loại cho phép nhiều lựa chọn và loại chỉ được phép chọn một khi làm việc với ChoiceGroup. Dạng thứ 3 là là dạng không tường minh. Các List không tường minh đuợc dùng để thể hiện một thực đơn các chọn lựa

Đoạn mã dưới đây minh họa việc sử dụng một danh sách không tường minh

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class ImplicitList extends MIDlet implements CommandListener

{

private Display display; // Reference to Display object

private List lsDocument; // Main list

private Command cmExit; // Command to exit

public ImplicitList()

{

display = Display.getDisplay(this);

// Create the Commands

cmExit = new Command("Exit", Command.EXIT, 1);

try

{

// Create array of image objects

Image images[] = {Image.createImage("/next.png"),

Image.createImage("/previous.png"),

Image.createImage("/new.png")};

// Create array of corresponding string objects

String options[] = {"Next", "Previous", "New"};

// Create list using arrays, add commands, listen for events

lsDocument = new List("Document Option:", List.IMPLICIT, options, images);

// If you have no images, use this line to create the list

// lsDocument = new List("Document Option:", List.IMPLICIT, options, null);

lsDocument.addCommand(cmExit);

lsDocument.setCommandListener(this);

}

catch (java.io.IOException e)

{

System.err.println("Unable to locate or read .png file");

}

}

public void startApp()

{

display.setCurrent(lsDocument);

}

public void pauseApp()

{

}

public void destroyApp(boolean unconditional)

{

}

public void commandAction(Command c, Displayable s)

{

// If an implicit list generated the event

if (c == List.SELECT_COMMAND)

{

switch (lsDocument.getSelectedIndex())

{

case 0:

System.out.println("Next selected");

break;

case 1:

System.out.println("Previous selected");

break;

case 2:

System.out.println("New selected");

break;

}

}

else if (c == cmExit)

{

destroyApp(false);

notifyDestroyed();

}

}

}

TextBox được dùng để cho phép nhập nhiều dòng. Thành phần TextBox và TextField có những ràng buộc giống nhau trong việc chỉ định loại nội dung được phép nhâp vào. Ví dụ ANY, EMAIL, URI… Dưới đây là phương thức dựng của một TextBox:

TextBox(String title, String text, int maxSize, int constraints) Dưới đây là đoạn mã minh họa cho việc sử dụng TextBox: import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class TextBoxTest extends MIDlet implements CommandListener

{

private Display display; // Reference to Display object

private TextBox tbClip; // Main textbox

private Command cmExit; // Command to exit

public TextBoxTest()

{

display = Display.getDisplay(this);

// Create the Commands. Notice the priorities assigned

cmExit = new Command("Exit", Command.EXIT, 1);

tbClip = new TextBox("Textbox Test", "Contents go here..",125, TextField.ANY);

tbClip.addCommand(cmExit);

tbClip.setCommandListener(this);

}

public void startApp()

{

display.setCurrent(tbClip);

}

public void pauseApp()

// Create an exclusive (radio) choice group

cgSound = new ChoiceGroup("Choose a sound", Choice.EXCLUSIVE);

// Append options, with no associated images

cgSound.append("Info", null);

cgSound.append("Confirmation", null);

cgSound.append("Warning", null);

cgSound.append("Alarm", null);

cgSound.append("Error", null);

cmExit = new Command("Exit", Command.EXIT, 1);

// Create Form, add components, listen for events

fmMain = new Form("");

fmMain.append(cgSound);

fmMain.addCommand(cmExit);

fmMain.setCommandListener(this);

fmMain.setItemStateListener(this);

}

public void startApp()

{

display.setCurrent(fmMain);

}

public void pauseApp()

{ }

public void destroyApp(boolean unconditional)

{ }

public void commandAction(Command c, Displayable s)

{

if (c == cmExit)

{

destroyApp(false);

notifyDestroyed();

}

}

public void itemStateChanged(Item item)

{

Alert al = null;

switch (cgSound.getSelectedIndex())

{

case 0:

al = new Alert("Alert sound", "Info sound",

null, AlertType.INFO);

break;

case 1:

al = new Alert("Alert sound", "Confirmation sound",

null, AlertType.INFO);

break;

case 2:

al = new Alert("Alert sound", "Warning sound",

null, AlertType.INFO);

break;

case 3:

al = new Alert("Alert sound", "Alarm sound",

null, AlertType.INFO);

break;

case 4:

al = new Alert("Alert sound", "Error sound",

null, AlertType.INFO);

break;

}

if (al != null)

{

// Wait for user to acknowledge the alert

al.setTimeout(Alert.FOREVER);

// Display alert, show main form when done

display.setCurrent(al, fmMain);

}

}

}

Thành phần Ticker đuợc dùng để thể hiện một đoạn chuỗi chạy theo chiều ngang. Tham số duy nhất của thành phần Ticker là đoạn văn bản được trình bày. Tốc độ và chiều cuốn được xác định bởi việc cài đặt trên thiết bị nào.

Phương thức dựng của Ticker

Ticker(String str)

Từ cây phân cấp các thành phần thể hiện trên thiết bị, ta thấy là thành phần Ticker không là lớp con của lớp ScreenTicker là một biến của lớp Screen. Điều này có nghĩa là một Ticker có thể được gắn vào bất cứ lớp con của lớp Screen bao gồm cả Alert

Dưới đây là đoạn mã minh họa việc sử dụng một Ticker

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class TickerTest extends MIDlet implements CommandListener

{

private Display display; // Reference to Display object

private List lsProducts; // Products

private Ticker tkSale; // Ticker

private Command cmExit; // Command to exit the MIDlet

public TickerTest()

{

display = Display.getDisplay(this);

cmExit = new Command("Exit", Command.SCREEN, 1);

tkSale = new Ticker("Sale: Real Imitation Cuban Cigars...10 for $10");

lsProducts = new List("Products", Choice.IMPLICIT);

lsProducts.append("Wicker Chair", null);

lsProducts.append("Coffee Table", null);

lsProducts.addCommand(cmExit);

lsProducts.setCommandListener(this);

lsProducts.setTicker(tkSale);

}

public void startApp()

{

display.setCurrent(lsProducts);

}

public void pauseApp()

{

}

public void destroyApp(boolean unconditional)

{

}

public void commandAction(Command c, Displayable s)

{

if (c == List.SELECT_COMMAND)

{

switch (lsProducts.getSelectedIndex())

{

case 0:

System.out.println("Chair selected");

break;

case 1:

System.out.println("Table selected");

break;

}

}

else if (c == cmExit)

{

destroyApp(true);

notifyDestroyed();

}

}

}

0