亚当爱上java 发表于 2013-1-27 12:58:36

J2ME用 Canvas做的联系人list

其中h表示画布的高度,title表示标题,index表示选中的下飚,还可以得到选中的标签和ID,支持触摸事件,这是个抽象类,要自己处理自己的事情,还需要实现doAction方法 即使点击OK按钮后需要执行的代码,界面做的太丑,背景色自己调吧。

import CanvasMidlet;import java.io.IOException;import java.util.Vector;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;public abstract class MyList extends Canvas{CanvasMidlet parent = null;Vector names = null;    Vector ids = null; int index = 0; // 当前选中操作目录索引 int h = 18; // 每条目录高度 int firstItem = 0; // 第一个ITEM int maxItemCount = this.getWidth() / h - 1; // 最多能显示多少个Item int startIndex = 0; // 开始索引 int endIndex = 0; // 结束索引 int tabIndex = 0; String title = ""; int topIndex = 0; String imageSrc = "";public MyList(String title,Vector names,Vector ids,int topIndex,String imageSrc) {this.names = names;this.ids= ids;this.topIndex = topIndex;this.title = title;this.imageSrc = imageSrc; }protected void paint(Graphics g) {this.clearBgColor(g);drawTitle(g,this.title);this.showList(g, names); } /*个人通讯录*/ public void showList(Graphics g, Vector names) {if (names.size() <= maxItemCount) {   endIndex = names.size();} else {   if (firstItem < 0)    firstItem = 0;   if (firstItem >= (names.size() - maxItemCount))    firstItem = names.size() - maxItemCount;   this.startIndex = firstItem;   endIndex = this.startIndex + this.maxItemCount;}this.drawList(g, startIndex, endIndex); }public void drawList(Graphics g, int st, int end) {Image image = null;//Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,//    Font.SIZE_SMALL);int fontColor = 0;int j = 1;for (int i = st; i < end; i++) {   int y1 = h * j + this.topIndex;   int y2 = h * j + 3 +this.topIndex;   j++;   if (i == index) {    fontColor = 0xffffff;    g.setColor(0, 85, 153);    g.fillRect(0, y1, getWidth(), h);   } else {    fontColor = 0;    g.setColor(0xffffff);    g.fillRect(0, y1, getWidth(), h);   }   try {    image = Image.createImage(this.imageSrc);   } catch (IOException e) {    System.out.println("找不到文件"+this.imageSrc);    e.printStackTrace();   }   g.drawImage(image, 0, y2, 0);   g.setColor(fontColor);//   g.setFont(f);   g.drawString(names.elementAt(i).toString(), 15, y1 + 1, Graphics.LEFT | Graphics.TOP);} } protected synchronized void keyPressed(int keyCode) {int action = getGameAction(keyCode);switch (action) {case DOWN: {   if (index == names.size() - 1) {    index = 0;    firstItem = 0;   } else {    if (maxItemCount < names.size()) {   if (index >= maxItemCount - 1)      firstItem++;    }    index++;   }   this.repaint();   break;}case UP: {   if (index == 0) {    index = names.size() - 1;    firstItem = names.size() - this.maxItemCount;   } else {    if (maxItemCount < names.size()) {   if (index == firstItem) {      firstItem--;   }    }    index--;   }   this.repaint();   break;}case FIRE: {   doAction();   break;}default: {   break;}} } protected void pointerPressed(int x, int y) {if(y<18) return; //选中标题什么都不做   int count = y / h - 1;   int clickIndex = firstItem + count;   if(clickIndex > names.size()) return ;//选中空白地方 什么都不做   if(clickIndex != index){      index = clickIndex;    this.repaint();   } } public abstract void doAction();/*获取选中项名称*/ public String getSelectedName(){return this.names.elementAt(index).toString(); }/*获取选中项ID*/ public int getSelectedID(){return Integer.parseInt(names.elementAt(index).toString()); }/*获取选中项索引*/ public int getSelectedIndex(){return index; }/*清除上一画布*/ public void clearBgColor(Graphics g){g.setColor(0xffffff);g.fillRect(0, 0, this.getWidth(), this.getHeight()); }public void drawTitle(Graphics g,String title){int width = Font.getDefaultFont().stringWidth(title);g.setColor(0);g.drawString(title, (this.getWidth() - width)/2 , 0, g.LEFT|g.TOP); //居中显示 }}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhouyong19871123/archive/2010/06/02/5642240.aspx
页: [1]
查看完整版本: J2ME用 Canvas做的联系人list