Desigining a Form in j2me [duplicate]

删除回忆录丶 提交于 2019-12-13 18:08:17

问题


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);
        }
    }
    }

}

回答1:


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

Public Menu() {

display = Display.getDisplay(this);
// Create a multiple choice list
lsPrefs = new List("Categories", List.MULTIPLE);

// Append options, with no associated images
lsPrefs.append("Category-1", null);
lsPrefs.append("Category-2", null);

cmNext = new Command("Next", Command.SCREEN,2);

// Add commands, listen for events

lsPrefs.addCommand(cmNext);
lsPrefs.setCommandListener(this);   
}

and in your commandAction class you can access the categories selected by

 public void commandAction(Command c, Displayable s){
  if (c == cmNext)
  {
  boolean selected[] = new boolean[lsPrefs.size()];

  // Fill array indicating whether each element is checked 
  lsPrefs.getSelectedFlags(selected);

  for (int i = 0; i < lsPrefs.size(); i++){
       if(selected[i])
                Alert(....);
  }


}



回答2:


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



来源:https://stackoverflow.com/questions/9031181/desigining-a-form-in-j2me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!