⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayelementeditor.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;

/**
 * ArrayElementEditor类是个数组字段编辑器,提供了数组字段的视图,和编辑命令。
 */
public class ArrayElementEditor extends MicroComponent implements CommandListener {
    //
    private Cardcase cardcase;
    private Contact card;
    private int field;
    private CardcaseMIDlet midlet;
    private MicroComponent backScreen;
    private int[] elemIndices;
    private Form elementView;   //数组元素视图
    
    private Command cmdSave = new Command("确定", Command.OK, 2);
    private Command cmdCancel = new Command("取消", Command.CANCEL, 1);
    
    //构造方法
    public ArrayElementEditor(Cardcase cardcase, Contact card, int field, CardcaseMIDlet midlet, MicroComponent backScreen) {
        this.cardcase = cardcase;
        this.card = card;
        this.field = field;
        this.midlet = midlet;
        this.backScreen = backScreen;
        
        String fieldLabel = cardcase.getFieldLabel(card, field);
        elementView = new Form(fieldLabel);
        elementView.addCommand(cmdSave);
        elementView.addCommand(cmdCancel);
        elementView.setCommandListener(this);
    }
    
    public void show() {
        elemIndices = cardcase.getArrayElementIndices(card, field);
        String[] a = card.getStringArray(field, 0);
        for(int i=0; i<elemIndices.length; i++) {
            int index = elemIndices[i];
            String elemLab = cardcase.getArrayElementLabel(card, field, index);
            elementView.append(new TextField(elemLab, a[index], 80, TextField.ANY));
        }
        Display.getDisplay(midlet).setCurrent(elementView);
    }
    
    public void commandAction(Command cmd, Displayable d) {
        if(cmd == cmdSave) {
            String[] a = new String[elemIndices.length];
            int n = elementView.size();
            for(int i=0; i<n; i++) {
                int index = elemIndices[i];
                a[index] = ((TextField)elementView.get(i)).getString().trim();
            }
            card.setStringArray(field, 0, PIMItem.ATTR_NONE, a);
        }
        backScreen.show();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -