高级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;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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();
-
- }
-
-
-
- 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(){
-
- 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);
-
- }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);
- }
-
-
-
-
-
-
- void keyUpAndDown(int keyCode) {
- if(this.numOfEls == 0)
- return;
- switch (keyCode) {
-
- case Platform.KEY_UP: {
- selectIndex--;
-
- break;
-
-
- }
- case Platform.KEY_DOWN: {
- selectIndex++;
-
- break;
- }
- }
- changeSelectIndex();
- }
-
-
-
-
- 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;
- }
- }
-
- }
|