创建listfield

采取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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值