版权所有,欢迎转载,转载请注明 : SinFrancis ?http://mdev.cc
?
BB中是没办法将Checkbox与ListField结合在一起的,不过我们可以自己写个,但也不是真的Checkbox,只是在功能上实现了而已,外观上看着像Checkbox。关键就是在使用 :?
选中状态字符 : Characters.BALLOT_BOX_WITH_CHECK?
非选中字符 :Characters.BALLOT_BOX
?
请看代码:关键地方?drawListRow()
当点击每个Item的时候,判断此行的状态,然后进行选中或者取消,将Characters.BALLOT_BOX_WITH_CHECK?
或者Characters.BALLOT_BOX画上去就Ok 了。
?
?
?
/* * CheckBoxListFieldSample.java * * Research In Motion, 2003-2005 * Confidential and proprietary. */package cc.mdev.seek;import net.rim.device.api.ui.*;import net.rim.device.api.ui.component.*;import net.rim.device.api.ui.container.*;import net.rim.device.api.system.*;import java.util.Vector;class CheckboxListField extends UiApplication implements ListFieldCallback{ private static final String[] _elements = {"First element", "Second element", "Third element", "Fourth element", "Fifth element"}; private Vector _listData = new Vector(); private ListField _checkList; private MenuItem _toggleItem; //A class to hold the Strings in the CheckBox and it's checkbox state (checked or unchecked). private class ChecklistData { private String _stringVal; private boolean _checked; ChecklistData() { _stringVal = ""; _checked = false; } ChecklistData(String stringVal, boolean checked) { _stringVal = stringVal; _checked = checked; } //Get/set methods. private String getStringVal() { return _stringVal; } private boolean isChecked() { return _checked; } private void setStringVal(String stringVal) { _stringVal = stringVal; } private void setChecked(boolean checked) { _checked = checked; } //Toggle the checked status. private void toggleChecked() { _checked = !_checked; } } /* public static void main(String args[]) { CheckboxListField theApp = new CheckboxListField(); theApp.enterEventDispatcher(); }*/ CheckboxListField() { //The menu item added to the screen when the _checkList field has focus. //This menu item toggles the checked/unchecked status of the selected row. _toggleItem = new MenuItem("Change Option", 200, 10) { public void run() { //Get the index of the selected row. int index = _checkList.getSelectedIndex(); //Get the ChecklistData for this row. ChecklistData data = (ChecklistData)_listData.elementAt(index); //Toggle its status. data.toggleChecked(); //Update the Vector with the new ChecklistData. _listData.setElementAt(data, index); //Invalidate the modified row of the ListField. _checkList.invalidate(index); } }; MainScreen myScreen = new MainScreen() { //Override the makeMenu method so we can add a custom menu item //if the checkbox ListField has focus. protected void makeMenu(Menu menu, int instance) { Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus(); if(focus == _checkList) { //The _checkList ListField instance has focus. //Add the _toggleItem MenuItem. menu.add(_toggleItem); } //Create the default menu. super.makeMenu(menu, instance); } }; //Add a title to the screen. myScreen.setTitle(new LabelField("Checkbox ListField sample")); _checkList = new ListField() { //Allow the space bar to toggle the status of the selected row. protected boolean keyChar(char key, int status, int time) { boolean retVal = false; //If the spacebar was pressed... if (key == Characters.SPACE) { //Get the index of the selected row. int index = getSelectedIndex(); //Get the ChecklistData for this row. ChecklistData data = (ChecklistData)_listData.elementAt(index); //Toggle its status. data.toggleChecked(); //Update the Vector with the new ChecklistData. _listData.setElementAt(data, index); //Invalidate the modified row of the ListField. invalidate(index); //Consume this keyChar (key pressed). retVal = true; } return retVal; } }; //Set the ListFieldCallback _checkList.setCallback(this); int elementLength = _elements.length; //Populate the ListField & Vector with data. for(int count = 0; count < elementLength; ++count) { //Even rows will be checked, odd will be unchecked. if (count % 2 == 0) { _listData.addElement(new ChecklistData(_elements[count], true)); } else { _listData.addElement(new ChecklistData(_elements[count], false)); } _checkList.insert(count); } //Add the ListField to the screen. myScreen.add(_checkList); //Push the screen onto the display stack. pushScreen(myScreen); } //ListFieldCallback impelmentation. //Draws the list row. public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) { //Get the ChecklistData for the current row. ChecklistData currentRow = (ChecklistData)this.get(list, index); StringBuffer rowString = new StringBuffer(); //If it is checked draw the String prefixed with a checked box, //prefix an unchecked box if it is not. if (currentRow.isChecked()) { rowString.append(Characters.BALLOT_BOX_WITH_CHECK); } else { rowString.append(Characters.BALLOT_BOX); } //Append a couple spaces and the row's text. rowString.append(Characters.SPACE); rowString.append(Characters.SPACE); rowString.append(currentRow.getStringVal()); //Draw the text. graphics.drawText(rowString.toString(), 0, y, 0, w); } //Returns the object at the specified index. public Object get(ListField list, int index) { return _listData.elementAt(index); } //Returns the first occurence of the given String, bbeginning the search at index, //and testing for equality using the equals method. public int indexOfList(ListField list, String p, int s) { //return listElements.getSelectedIndex(); return _listData.indexOf(p, s); } //Returns the screen width so the list uses the entire screen width. public int getPreferredWidth(ListField list) { return Graphics.getScreenWidth(); }}?
?