首页 » 黑莓开发 » 创建listfield

创建listfield

4383 1

采取listfield+keylistener来做程序的option菜单.不知道系统中option菜单是不是通过这个方法来实现的,轨迹球的listener应该可以照样搬上去.

import java.util.*;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class SampleListFieldCallback extends UiApplication{
private ListField myList;
public static void main(String[] args) {
SampleListFieldCallback app = new SampleListFieldCallback();
app.enterEventDispatcher();
}
private static class ListCallback implements ListFieldCallback {
private Vector listElements = new Vector();
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String text = (String)listElements.elementAt(index);
g.drawText(text, 0, y, 0, w);
}
public Object get(ListField list, int index) {
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String p, int s) {
return listElements.indexOf(p, s);
}
public int getPreferredWidth(ListField list) {
return Graphics.getScreenWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void rase() {
listElements.removeAllElements();
}
}
public SampleListFieldCallback() {
MainScreen mainScreen = new MainScreen();
mainScreen.addKeyListener(new KeyListener() {
public boolean keyChar(char key, int status, int time) {
if (key == Characters.ESCAPE) return false;
if (key == Characters.ENTER) {
System.out.println(myList.getSelectedIndex());
if (myList.getSelectedIndex() == 0)
pushScreen(new ascreen());
else if (myList.getSelectedIndex() == 1)
pushScreen(new ascreen2());
else if (myList.getSelectedIndex() == 2)
pushScreen(new ascreen3());
}
return true;
}
public boolean keyDown(int keycode, int time) {
return false;
}
public boolean keyUp(int keycode, int time) {
// UiApplication.getUiApplication().pushScreen(new ascreen());
return false;
}
public boolean keyRepeat(int keycode, int time) {
return false;
}
public boolean keyStatus(int keycode, int time) {
return false;
}
});
myList = new ListField();
ListCallback myCallback = new ListCallback();
myList.setCallback(myCallback);
String fieldOne = "ListField one";
String fieldTwo = "ListField two";
String fieldThree = "ListField three";
myList.insert(0);
myCallback.insert(fieldOne, 0);
myList.insert(1);
myCallback.insert(fieldTwo, 1);
myList.insert(2);
myCallback.insert(fieldThree, 2);
mainScreen.add(myList);
pushScreen(mainScreen);
}
}
class ascreen extends MainScreen {
public ascreen (){
super();
LabelField title = new LabelField("onedetial",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
}
}
class ascreen2 extends MainScreen {
public ascreen2 (){
super();
LabelField title = new LabelField("twodetial",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
}
}
class ascreen3 extends MainScreen {
public ascreen3 (){
super();
LabelField title = new LabelField("threedetial",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
}
}

文章评分1次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2009/10/bb-develop-create-listfield/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关:


1 Comment

  1. mooc :

    不明觉厉。。

载入分页评论...