高級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;
- }
- }
-
- }
|