📄 choicegroup.java
字号:
package com.cuit.lui;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ChoiceGroup extends Component {
public static int MULTIPLE_CHOICE = 0;
public static int SINGLE_CHOICE = MULTIPLE_CHOICE + 1;
public static int POP_UP = SINGLE_CHOICE + 1;
private int width = LScreen.screenWidth;
private int height = 0;
private int borderColor = LUIConfig.DEFAULT_ROW_BORDER_COLOR;
private Vector items = new Vector();
private Vector ifSelected = new Vector();
private int selectedIndex = -1;
private int itemPointer = -1;
private Vector selectedIndexes = new Vector();
private int choiceType;
private int focusy;
private Image unselected = null, selected = null;
private int imageWidth = 0, imageHeight = 0, textWidth = 0;
public ChoiceGroup(int type) {
super.ifCommand = true;
choiceType = type;
if (choiceType == MULTIPLE_CHOICE || choiceType == SINGLE_CHOICE) {
loadRes();
imageWidth = selected.getWidth();
imageHeight = selected.getHeight();
} else if (choiceType == POP_UP) {
}
adjustLayout();
}
private void loadRes() {
try {
unselected = Image.createImage("/com/cuit/lui/unselected.png");
selected = Image.createImage("/com/cuit/lui/selected.png");
} catch (Exception e) {
LScreen.error("choicegroup loading images error!");
e.printStackTrace();
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public boolean keyEvent(int keyCode) {
needRepaint = false;
int k = keyCode;
// System.out.println(k);
if (k == LUIConfig.UP) {
needRepaint = true;
if (itemPointer == -1) {
itemPointer = items.size() - 1;
} else {
if ((itemPointer - 1) >= 0) {
itemPointer--;
} else {
setBorder(false);
setOnFocus(false);
}
}
} else if (k == LUIConfig.DOWN) {
needRepaint = true;
if ((itemPointer + 1) < items.size()) {
itemPointer++;
} else {
setBorder(false);
setOnFocus(false);
}
} else if (k == LUIConfig.FIRE) {
needRepaint = true;
System.out.println("-----------");
if (choiceType == MULTIPLE_CHOICE) {
boolean b = ((Boolean) ifSelected.elementAt(itemPointer))
.booleanValue();
ifSelected.setElementAt(new Boolean(!b), itemPointer);
selectedIndexes.addElement(new Integer(itemPointer));
} else if (choiceType == SINGLE_CHOICE) {
if (itemPointer == selectedIndex) {
boolean b = ((Boolean) ifSelected.elementAt(itemPointer))
.booleanValue();
ifSelected.setElementAt(new Boolean(!b), itemPointer);
} else {
if (selectedIndex >= 0) {
ifSelected.setElementAt(new Boolean(false),
selectedIndex);
}
ifSelected.setElementAt(new Boolean(true), itemPointer);
selectedIndex = itemPointer;
}
} else if (choiceType == POP_UP) {
} else {
LScreen.error("choiceType invalid!");
}
} else {
needRepaint = false;
return needRepaint;
}
return needRepaint;
}
public void paint(Graphics g) {
if (g == null) {
LScreen.error("choicegroup graphics is null!");
return;
} else {
LScreen.console("painting choicegroup...");
if (isBorder) {
g.setColor(borderColor);
g.drawRect(posx, posy, width, height);
}
int imagex = posx;
int imagey = posy;
int textx = posx + imageWidth + 5;
int texty = posy;
for (int i = 0; i < items.size(); i++) {
if (((Boolean) (ifSelected.elementAt(i))).booleanValue()) {
g.drawImage(selected, imagex, imagey, 0);
} else {
g.drawImage(unselected, imagex, imagey, 0);
}
Vector strings = (Vector) items.elementAt(i);
int stringnb = strings.size();
if (itemPointer == i) {
int rectx = textx;
int recty = texty;
focusy = recty;
int h = stringnb * LScreen.wrapTextFont.getHeight()
+ (stringnb - 1) * 1;
int w = textWidth;
g.setColor(LUIConfig.DEFAULT_TEXTBG_COLOR);
g.fillRect(rectx, recty, w, h);
}
for (int j = 0; j < stringnb; j++) {
String s = (String) strings.elementAt(j);
g.setColor(0x000000);
g.drawString(s, textx, texty + j
* (LScreen.wrapTextFont.getHeight() + 1), 0);
}
texty += stringnb * LScreen.wrapTextFont.getHeight()
+ (stringnb - 1) * 1;
imagey = texty;
}
}
}
public void append(String s) {
if (s == null)
return;
Vector strings = LScreen.wrapText(s, textWidth);
items.addElement(strings);
ifSelected.addElement(new Boolean(false));
if (strings.size() > 1) {
int size = strings.size();
int deltah = size * LScreen.wrapTextFont.getHeight() + (size - 1)
* 1;
height += deltah;
} else {
height += imageHeight;
}
}
public void setOnFocus(boolean b) {
super.setOnFocus(b);
if (!b) {
itemPointer = -1;
isBorder = false;
}
}
public void setWidth(int w) {
width = w;
}
public void adjustLayout() {
textWidth = width - imageWidth - 5;
}
public int getFocusy() {
return focusy;
}
public void reset() {
itemPointer = -1;
selectedIndex = -1;
selectedIndexes.removeAllElements();
setOnFocus(false);
return;
// for (int i = 0; i < ifSelected.size(); i++) {
// ifSelected.setElementAt(new Boolean(false), i);
// }
}
public int getSelectedIndex(){
if(choiceType==MULTIPLE_CHOICE){
LScreen.error("the method is applicable for multiple choice");
return -1;
}else{
return selectedIndex;
}
}
public boolean ifSelected(int n){
if(n<0||n>size()-1){
LScreen.error("IndexOutOfBounds: elementNum is invalid!");
return false;
}
return ((Boolean)(ifSelected.elementAt(n))).booleanValue();
}
public int getSelectedFlags(boolean[] selectedArray_return){
if(selectedArray_return==null){
LScreen.error("selectedArray_return can't be null");
return -1;
}
if(selectedArray_return.length<ifSelected.size()){
LScreen.error("selectedArray_return can't be shorter than the size of the ChoiceGroup");
return -1;
}
int i;
for(i=0;i<ifSelected.size();i++){
boolean b=((Boolean)ifSelected.elementAt(i)).booleanValue();
selectedArray_return[i]=b;
}
return i+1;
}
public void setSelectedIndex(int elementNum,boolean selected){
if(elementNum<0||elementNum>size()-1){
LScreen.error("IndexOutOfBounds: elementNum is invalid!");
return;
}
if(choiceType==MULTIPLE_CHOICE){
ifSelected.setElementAt(new Boolean(selected), elementNum);
}else{
if(!selected){
return;
}
if(selectedIndex!=elementNum){
ifSelected.setElementAt(new Boolean(false), selectedIndex);
ifSelected.setElementAt(new Boolean(selected), elementNum);
}
}
}
public int size(){
return items.size();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -