在 j2me 中设计表单
可能的重复:
在 j2me 中设计表单
我创建了一个带有 str 1 和 str 2 作为 StringItem 的表单并将它们添加到表单中。
然后我向 str1 和 str2 添加了 addCommand() 方法,当我单击“下一步”按钮时,当单击类别 1 时,它应该显示警报为类别 1;当单击类别 2 StringItem 时,它应该显示警报为类别 2。
请提出解决方案
我应该在 Command Action () 方法的 if 循环中编写什么,以便当我单击 str1 时,它应该显示类别 1,而当单击 str2 ... 时,它应该显示类别 2?
代码如下,上述问题在 commandAction
方法中的注释中
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Menu extends MIDlet implements CommandListener {
Command Next ;
Display display;
Form form;
StringItem str1,str2;
public Menu() {
// TODO Auto-generated constructor stub
str1=new StringItem("1. ", "Category 1");
str2=new StringItem("2.", "Category 2");
form=new Form("Menu");
form.append(str1);
form.append(str2);
Next=new Command("Next",Command.SCREEN, 1);
str1.addCommand(Next);
str2.addCommand(Next);
form.addCommand(Next);
form.setCommandListener(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=Display.getDisplay(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==Next)
{
if(/* What should I write here? */)
{
Alert alert=new Alert(null, "This is ", null, AlertType.INFO);
display.setCurrent(alert, form);
}
}
}
}
Possible Duplicate:
Desigining a Form in j2me
I have created a Form with str 1 and str 2 as StringItem and added them on Form.
Then i have added addCommand() method to str1 and str2 and when I click on Next button it should display an alert as Category 1 when clicked on category 1 and When category 2 when clicked on category 2 StringItem.
Please suggest a solution
What should I write in if loop of Command Action () method so that When I click on str1 it should display Category 1 and when on str2 ... category 2?
Code is below, above question is in comment within commandAction
method
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Menu extends MIDlet implements CommandListener {
Command Next ;
Display display;
Form form;
StringItem str1,str2;
public Menu() {
// TODO Auto-generated constructor stub
str1=new StringItem("1. ", "Category 1");
str2=new StringItem("2.", "Category 2");
form=new Form("Menu");
form.append(str1);
form.append(str2);
Next=new Command("Next",Command.SCREEN, 1);
str1.addCommand(Next);
str2.addCommand(Next);
form.addCommand(Next);
form.setCommandListener(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=Display.getDisplay(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==Next)
{
if(/* What should I write here? */)
{
Alert alert=new Alert(null, "This is ", null, AlertType.INFO);
display.setCurrent(alert, form);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅 Form 需要 Next 命令,str1 和 str2 不需要。事实上,您可以有一个包含类别的列表。因此,在 Menu() 构造函数
和 commandAction 类中,您可以访问由
The Next command is required only for the Form and is not required for str1 and str2 . In fact you can have a List which comprises of categories . So in your Menu() constructor
and in your commandAction class you can access the categories selected by
尝试使用 ChoiceGroup - 以下是如何使用它的示例 http://www.java-tips.org/java-me-tips/midp/how-to-use-choicegroup-in-j2me.html
Try using ChoiceGroup - Here's an example as to how to use it http://www.java-tips.org/java-me-tips/midp/how-to-use-choicegroup-in-j2me.html