微科社区,轻松开发从此开始! 请登陆 免费注册

微科社区

当前位置:首页 > Java平台 > J2ME >

多个StringItem添加在form后只显示一个

时间:2016-12-19 04:01  浏览:努力统计中...
想把每次选择后的选项显示在StringItem,定义了四个StringItem却只显示一个 [java code]import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class ChoiceGroupMIDlet extends MIDlet implements Comman
想把每次选择后的选项显示在StringItem,定义了四个StringItem却只显示一个
[java code]import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ChoiceGroupMIDlet extends MIDlet implements CommandListener,
ItemStateListener {
private Display display;
private Command exitCommand;
private Form myform;
private ChoiceGroup myExclusiveChoiceGroup;
private ChoiceGroup myMultipleChoiceGroup;
private ChoiceGroup myPopupChoiceGroup;
private StringItem myChoice;
private Image img = null;
private StringItem str01;
private StringItem str02;
private StringItem str03;
private int index;

public ChoiceGroupMIDlet() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
exitCommand = new Command("退出",Command.SCREEN,1);
myform = new Form("ChoiceGroup演示");
try{
img = Image.createImage("/pic.png");
}catch(Exception e){
myform.append("加载图像文件失败");
}
myExclusiveChoiceGroup = new ChoiceGroup("选择性别",ChoiceGroup.EXCLUSIVE);
myMultipleChoiceGroup = new ChoiceGroup("选择学校",ChoiceGroup.MULTIPLE);
myPopupChoiceGroup = new ChoiceGroup("选择婚姻状况",ChoiceGroup.POPUP);
myExclusiveChoiceGroup.append("男", img);
myExclusiveChoiceGroup.append("女", img);
myMultipleChoiceGroup.append("南开大学", img);
myMultipleChoiceGroup.append("天津大学", img);
myMultipleChoiceGroup.append("清华大学", img);
myMultipleChoiceGroup.append("北京大学", img);
myPopupChoiceGroup.append("未婚", img);
myPopupChoiceGroup.append("已婚", img);

myChoice = new StringItem("您选择的内容是:","");
str01 = new StringItem("性别","男");
str02 = new StringItem("学校","");
str03 = new StringItem("婚姻","未婚");

myform.append(myExclusiveChoiceGroup);
myform.append(myMultipleChoiceGroup);
myform.append(myPopupChoiceGroup);

myform.append(myChoice);
//myform.append(str01);
myform.append(str02);
myform.append(str03);
index = myform.append(str01);
System.out.print("插入的位置是:" + index);

myform.addCommand(exitCommand);
myform.setCommandListener(this);
myform.setItemStateListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(myform);
}

public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c == exitCommand){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}

}

public void itemStateChanged(Item item) {
// TODO Auto-generated method stub
  if(item == myExclusiveChoiceGroup){
  int selected = myExclusiveChoiceGroup.getSelectedIndex();
  String temp = myExclusiveChoiceGroup.getString(selected);
  str01.setText(temp);
  }
  if(item == myMultipleChoiceGroup){//重复出现之前已经选择的选项
  boolean selected;
  String temp = "";
  for(int i = 0;i < myMultipleChoiceGroup.size();i++){
  selected = myMultipleChoiceGroup.isSelected(i);
  if(selected){
  temp += myMultipleChoiceGroup.getString(i);
  }
  }
  str02.setText(temp);
  }
  if(item == myPopupChoiceGroup){
  int selected = myPopupChoiceGroup.getSelectedIndex();
  String temp = myPopupChoiceGroup.getString(selected);
  str03.setText(temp);
  }
}

}
[\java code]

你定义了鼠标进行选择的事件吗???

把4个内容放在1个form中;你确定都添加了?还是你要把字符串连在一起没连上?



都添加啦,可是运行以后只显示了第一个,后面的都没有显示



定义了事件监听和按钮监听啊

把4个内容放在1个form中;你确定都添加了
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线------