lwuit-List控件使用
2010-11-29 17:55
369 查看
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合起来使用真的是要下一番功夫
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合起来使用真的是要下一番功夫
相关文章推荐
- lwuit-List控件使用
- lwuit-List控件使用
- lwuit-tab与list配合使用
- LWUIT 上使用List实现表格
- LWUIT 上使用List实现表格
- DevExpress.XtraTreeList.TreeList (树控件使用说明)
- 表友汇PC端技术解析_CheckBoxList控件的使用
- Tip:在使用AjaxControlTookit的控件时响应事件
- 使用复杂绑定完成 CheckBoxList 和 RadioButtonList 控件的绑定
- VC List Control控件高级使用
- TValueListEditor 控件使用方法
- (二)android中在xml文件中使用View在某个控件的上方画一条线;android:listSelector的属性说明;android:visibility="gone"
- ASP.NET AJAX Tookit TabContainer 控件使用常见错误及解决方法
- ASP.NET中BulletedList列表控件使用及详解
- InteWidgetTookit现有三个控件的使用方法
- Tip:在使用AjaxControlTookit的控件时响应事件
- atitit.提升开发效率---使用服务器控件生命周期 asp.net 11个阶段 java jsf 的6个阶段比较
- 递归方法遍历树节点,使用Dev的TreeList控件
- 使用 LWUIT 的风格、主题和 Painter 特性
- egret 中list控件使用问题