您的位置:首页 > 产品设计 > UI/UE

lwuit-List控件使用

2009-07-01 11:20 316 查看
List控件的使用是很频繁的,今天就介绍一下Lwuit中List的使用方法:

private String globalstrURL = "";

public void initform() {
removeAll();
removeAllCommands();
String[][] strAccessories = EsionData.getInstance()
.GetSiteList("05342");
this.addCommand(new Command("返回", 0) {
public void actionPerformed(ActionEvent ev) {
FormFactory.getInstance().getMainForm().show();
}
});
if (strAccessories != null) {
SiteListInfo[] accessoryinfoarray = new SiteListInfo[strAccessories.length];
Image img = getRes().getImage("YdFj.gif");
for (int i = 0; i < strAccessories.length; i++) {
accessoryinfoarray[i] = new SiteListInfo(strAccessories[i][0],
strAccessories[i][1], img);
}
// 设置使得list的选中状态的层能够自适应的足够撑开
setLayout(new BorderLayout());
setScrollable(false);
//创建List并传入相应的数据
addComponent(BorderLayout.CENTER, createList(accessoryinfoarray,
List.VERTICAL, new ContactsRenderer()));
this.addCommand(new Command("确定", 1) {
public void actionPerformed(ActionEvent ev) {
try {
EsionMIDlet.midlet.platformRequest(globalstrURL);
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
}
}
});
} else
addComponent(new Label("网络出现异常,没有找到任何资讯站点!"));
}

// List初始化
private List createList(final SiteListInfo[] SiteLists, int orientation,
ListCellRenderer renderer) {
final List list = new List(SiteLists);
list.getStyle().setBgTransparency(0);
list.setListCellRenderer(renderer);
list.setOrientation(orientation);
// list列表选择焦点监听事件
list.addSelectionListener(new SelectionListener() {
public void selectionChanged(int oldSelected, int newSelected) {
globalstrURL = SiteLists[list.getSelectedIndex()].siteurl;
}
});
list.addFocusListener(new FocusListener() {
// list获得焦点
public void focusGained(Component cmp) {
globalstrURL = SiteLists[list.getSelectedIndex()].siteurl;
}

// list失去焦点
public void focusLost(Component cmp) {
globalstrURL = "";
}
});
//list事件 ---手机中间按键相应的事件
list.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
try {
EsionMIDlet.midlet.platformRequest(globalstrURL);
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
}
}
});
return list;
}

// list数据绑定
class ContactsRenderer extends Container implements ListCellRenderer {
private Label name = new Label("");
private Label pic = new Label("");
private Label focus = new Label("");

public ContactsRenderer() {
setLayout(new BorderLayout());
//设置list每一列的图标
addComponent(BorderLayout.WEST, pic);
Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
name.getStyle().setBgTransparency(0);
name.getStyle().setFont(
Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_MEDIUM));
//设置list每一列内容
cnt.addComponent(name);
addComponent(BorderLayout.CENTER, cnt);
// 这里设置选中颜色,也可不设置通过Lwuit的资源管理器设置选中颜色也可
// 不过不能跟底色重合,否则看不见效果,其实这也是相对于多普达的手机才需要这样设置
focus.getStyle().setBgColor(0xffffff);
focus.getStyle().setBgTransparency(100);
}

public Component getListCellRendererComponent(List list, Object value,
int index, boolean isSelected) {
SiteListInfo sitelist = (SiteListInfo) value;
name.setText(sitelist.getsitename());
pic.setIcon(sitelist.getpic());
return this;
}

public Component getListFocusComponent(final List list) {
return focus;
}
}
//创建一个类用来放置List中需要的数据,类似的如需要传值到另一个窗体,或者list中列显示的数据
class SiteListInfo {
private String sitename;
private String siteurl;
private Image pic;

public SiteListInfo(String sitename, String siteurl, Image pic) {
this.sitename = sitename;
this.siteurl = siteurl;
this.pic = pic;
}

public String getsitename() {
return sitename;
}

public String getsiteurl() {
return siteurl;
}

public Image getpic() {
return pic;
}
}
需要注意的事项及其说明都标注在源码上,List的使用还是比较简单的,不过Tab选项卡与List合起来使用真的是要下一番功夫,后续将会有一篇Tab与List结合使用的文章。

上图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: