【问题标题】:how to create TextFields dynamically in j2me?如何在 j2me 中动态创建 TextField?
【发布时间】:2011-09-15 11:44:46
【问题描述】:

我们正在 j2me 中开发移动应用程序。在我的应用程序中,我们使用 TextField 和 Form 中的其他一些控件。在这里,我的问题是我想根据用户的凭据动态创建 TextField。例如,如果输入了 Manager,然后我想创建某些 TextField(基于 Manager Selection)以从 Manager 获取输入。否则,我只想创建小于 Manager TextField 的 TextField。

如何动态创建文本字段...

比如这样……

int userSelection=10;

for(int i=0;i<userSelection;i++)
    TextField text=new TextField("Some Name",null);

这里,我们的问题是,

我想创建具有不同名称的 TextField...

请指导我摆脱这个问题...

【问题讨论】:

  • 您使用的是什么 API?例如,在 midp lcdui TextField 中根本没有构造函数可以允许 new TextField("Some Name",null)
  • 是的,它在 MIDP 2.0 中不可用。看我的回答。并像那样使用。

标签: java java-me midp lcdui


【解决方案1】:

创建 TextField 数组并从数组索引中引用。

TextField[] textFields = new TextField[10];
for (int i = 0; i < textFields.length; i++) {
     textFields[0] = new TextField(label, text, maxSize, constraint);
}

【讨论】:

    【解决方案2】:

    使用正确的参数构造 TextField 后,代码可能如下所示

    import javax.microedition.lcdui.TextField;
    import java.util.Vector;
    // ...
        Vector newTextFields(int userSelection) {
            // neither List nor generics in midp sorry
    
            final int MAX_SIZE = 42;
            final Vector list = new Vector();
            for(int i=0; i < userSelection; i++) {
                list.addElement(new TextField("Name #" + i, null,
                        MAX_SIZE, TextField.ANY);
            }
            return list;
        }
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多