??????? 高级UI中的List控件对于广大应用来说是足够的。但有些特别需求的功能确不得不自己开发,比如实现没一行字体颜色不同,字体样式不同,还有排版等方面问题时候则要自己动手实现一个了。下面把我在项目中学习到得经验与大家分享下。
?????? 但是客户有个需求,说你这个List需要翻页,我要求输入什么键你进行上下翻页。我要求在每一行字体里面包含一些不同颜色得字,根据XP,好我拥抱需求。所以让我们来看下怎么修改程序得。
????? 注意在看这篇文章之钱,请稍微留意下在下得前面几篇文章。谢谢,^_^
????? 代码如下,我会加比较多得注释
java 代码
- /********************************************************************?
- ?*??
- ?*?版权说明,此程序仅供学习参考。不能用于商业?
- ?*??
- ?********************************************************************/??
- package?org.pook.ui;??
- ??
- import?java.util.Vector;??
- ??
- import?javax.microedition.lcdui.Graphics;??
- import?javax.microedition.lcdui.Image;??
- ??
- import?org.pook.ui.core.Platform;??
- import?org.pook.ui.util.GraphicsUtil;??
- ???
- ??
- /**?
- ?*?类名:List.java?
?? - ?*?编写日期:?2006-10-14?
? - ?*?程序功能描述:实现Part部件,而获取可按照自己要求得效果得List
? - ?*?Demo:?
? - ?*?Bug:?
? - ?*??
- ?*?程序变更日期?:
?? - ?*?变更作者?:
?? - ?*?变更说明?:
? - ?*??
- ?*?@author?wuhua?
?? - ?*/??
- public?class?List?extends?Part?{??
- ????Image?icon;??
- ????Vector?items;????????
- ????int?numOfEls;???
- ????int?paintSize;??
- ????int?space;??
- ????/**?开始索引?*?*/??
- ????private?int?startIndex;??
- ????
- ????public?List(Image?icon)?{??
- ????????super(0,?21,?Platform.WIDTH,?Platform.HEIGHT?-?41);??
- ????????this.icon?=?icon;??
- ????????items?=?new?Vector();??
- ???????????
- ????}??
- ???
- ?? /** 主要是根据屏幕变化而改变各个StringItem的位置跟当前屏幕可以显示StringItem的大小*?*/?
- ?
- ????public?void?changeViewAndSize(){??
- ????????if?(Platform.HEIGHT?-?20?>?view[HEIGHT])?{??
- ????????????view[HEIGHT]?=?Platform.HEIGHT?-?41;??
- ????????????space?=?font.getHeight()?+?2;??
- ????????????paintSize??=?view[HEIGHT]?/?space;??
- ????????}??
- ????}??
- ???
- ????public?void?append(Vector?items){??
- ????????if(items?==?null)??
- ????????????return;??
- ????????this.items?=?items;??
- ????????this.numOfEls?=?items.size();??
- ????}??
- ??
- ????public?void?append(String?stringItem){??
- ????????this.items.addElement(stringItem);??
- ????????this.numOfEls?=?items.size();??
- ????}??
- ??????
- ??????
- ????public?void?insert(String?stringItem){??
- ????????this.items.insertElementAt(stringItem,0);??
- ????????this.numOfEls?=?items.size();??
- ????}??
- ??????
- ????public?int?getSelectIndex(){??
- ????????return?this.selectIndex;??
- ????}??
- ??????
- ????public?String?getSelectString(){??
- ????????//System.out.println(this.numOfEls);??
- ????????return?(String)?this.items.elementAt(selectIndex+startIndex?);??
- ????}??
- ??????
- ????public?void?paint(Graphics?g)?{??
- ????????changeViewAndSize();??
- ????????GraphicsUtil.fillScreen(g,?this.bgColor,?view[X],?view[Y],?view[WIDTH],?view[HEIGHT]);??
- ????????paintStrings(g);??
- ????}??
- ??
- ????private?void?paintStrings(Graphics?g)?{??
- ??????
- ????????if?(items.size()?==?0)??
- ????????????return;??
- ????????int?size?=?this.paintSize?>?this.numOfEls??this.numOfEls:this.paintSize?+?startIndex;??
- ??????????
- ????????paintSelect(g,?view[Y]?+?space?*?selectIndex?+?2?);??
- ??????????
- ????????g.setColor(this.fontColor);??
- ??????????
- ????????for(int?i?=startIndex,j=0;?i<?size;?i++,?j++){??
- ???????????????
- ????????????String?it?=?(String)?items.elementAt(i);??????????
- ??????????????
- ????????????if(this.selectIndex?==?j){??
- ????????????????it?=?(String)?items.elementAt(selectIndex+startIndex);??
- ????????????????//this.select.paint(view[X],?height,?view[WIDTH],it.getItemHeight(),g);??
- ????????????}else{??
- ???????????????????
- ????????????}??
- ????????????GraphicsUtil.darwString(g,it,?view[X]?+?10,?view[Y]?+?space?*j?+?2);??
- ????????????//?变化的高度??
- ???????????????
- ????????}????
- ????}??
- ????private?void?paintSelect(Graphics?g,?int?height)?{??
- ????????g.setColor(0x909090);??
- ????????g.fillRect(view[X],?height,?view[WIDTH],?space);??
- ????}??
- ??
- ????public?void?onClick(int?keyCode)?{??
- ????????keyUpAndDown(keyCode);??
- ????}??
- ??
- ????/**?
- ?????*?内部实现按钮向上向下时候的动作,有具体类的keyPress调用.?
- ?????*??
- ?????*?@param?keyCode?
- ?????*/??
- ????void?keyUpAndDown(int?keyCode)?{??
- ????????if(this.numOfEls?==?0)??
- ????????????return;??
- ????????switch?(keyCode)?{??
- ??????
- ????????case?Platform.KEY_UP:?{??
- ????????????selectIndex--;??
- ???????????????
- ?????????break;??
- ????????????????
- ???????????????
- ????????}??
- ????????case?Platform.KEY_DOWN:?{??
- ????????????selectIndex++;??
- ???????????????
- ?????????????break;??
- ????????}??
- ????????}??
- ????????changeSelectIndex();??
- ????}??
- ??????
- ????/**?
- ?????*?判断当前选择条是否到了底部,经过用户的选择,这些选择条会不断的变化
?变化的依据是当selectPosition?>=? - ?????*?viewPart[HEIGHT]*/??
- ????private?void?changeSelectIndex(){??
- ????????int?num?=?(this.paintSize?<?numOfEls)?paintSize:numOfEls;//取可显示的菜单项数目??
- ????????if?(selectIndex>num-1)??
- ????????{??
- ????????????startIndex++;??
- ????????????selectIndex=(byte)(num-1);??
- ????????}??
- ??????????
- ????????if?(selectIndex?<?0)??
- ????????{??
- ????????????if?(startIndex>0)??
- ????????????{??
- ????????????????selectIndex?=0;??
- ????????????????startIndex--;??
- ????????????}??
- ????????????else??
- ????????????{??
- ????????????????startIndex?=?numOfEls-num;??
- ????????????????selectIndex=num-1;??
- ????????????}??
- ??????????????
- ????????}??
- ????????if?(startIndex+?selectIndex?>?numOfEls?-1)??
- ????????{??
- ????????????startIndex=?0;??
- ????????????selectIndex?=?0;??
- ????????}??
- ????}??
- ??
- }??
1 楼 为你而来 2006-11-22
强人,多谢啦!!
2 楼 ouspec 2006-11-22
恩,不错