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结合使用的文章。
上图:
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结合使用的文章。
上图:
相关文章推荐
- lwuit-List控件使用
- lwuit-List控件使用
- lwuit-tab与list配合使用
- LWUIT 上使用List实现表格
- LWUIT 上使用List实现表格
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- Dev 控件treelist等滚动条使用
- VC++中List Control控件的使用方法介绍
- 使用DevExpress的TreeList控件的效果和实现代码
- BulletedList控件的使用
- 关于DevExpress.XtraTreeList.TreeList控件的使用——给TreeList添加RepositoryItem以实现文本编辑功能
- MFC中ListControl控件的使用
- 递归方法遍历树节点,使用Dev的TreeList控件
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- VC中在对话框上使用Rich Edit控件前一定要用AfxInitRichEdit()初始化RichEdit环境
- 解读 LWUIT 之四:LWUIT 控件(中)
- 如何使用RequiredFieldValidator控件验证CheckBoxList
- VC LIST控件使用方法
- delphi valuelisteditor控件的使用
- DevExpress控件使用(ImageCollection,TreeList)