【问题标题】:Codename One back command update form data代号一回命令更新表格数据
【发布时间】:2020-11-22 18:41:04
【问题描述】:

我有一个使用返回命令返回开始屏幕的 Android 应用程序。

开始屏幕有一个标签,里面有一个数字,我想在使用返回命令时更新它。

我可以用 back 命令中的代码找出一个解决方案,但我不知道我的方法是否是最好的,因为 ClassOne 会被加载两次。

这是我已有的代码:

public class ClassOne {

    public ClassOne(ClassPojo classPojo) {

        // I want to change the text of this label when calling the back command
        labelOne.setText(classPojo.getStringTest());
        formOne.show();
    }

}

public class ClassTwo {
        
    public ClassTwo(Form a , ClassPojo classPojo) {     
        Command back = new Command("A") {
            @Override
            public void actionPerformed(ActionEvent evt) {

                // I am adding the new value for the label here inside the back command

                classPojo.setStringTest("testing");
                a.showBack(); 
                new ClassOne(classPojo);
            }
        };
        formTwo.setBackCommand(back);
    }

【问题讨论】:

    标签: codenameone back


    【解决方案1】:

    我不确定问题是什么,你的例子有点笼统。但是,没有重新创建 startScreen 表单实例的完整最小示例是:

            Form startScreen = new Form("Start screen", BoxLayout.y());
            Wrapper<Integer> count = new Wrapper<>(1);
            Label numberLabel = new Label(count.get() + "");
            Button button1 = new Button("Go to Form 2");
            startScreen.addAll(numberLabel, button1);
            startScreen.show();
    
            button1.addActionListener(l -> {
                Form form2 = new Form("Form 2", BoxLayout.y());
                Label label = new Label("Use the back button");
                form2.add(label);
                form2.getToolbar().setBackCommand("Back", Toolbar.BackCommandPolicy.ALWAYS, ll -> {
                    count.set(count.get() + 1);
                    numberLabel.setText(count.get() + "");
                    startScreen.showBack();
                });
                form2.show();
            });
    

    如果您甚至不想重新创建 form2 实例,那么您可以这样做:

            Form startScreen = new Form("Start screen", BoxLayout.y());
            Wrapper<Integer> count = new Wrapper<>(1);
            Label numberLabel = new Label(count.get() + "");
            Button button1 = new Button("Go to Form 2");
            startScreen.addAll(numberLabel, button1);
            startScreen.show();
    
            Form form2 = new Form("Form 2", BoxLayout.y());
            Label label = new Label("Use the back button");
            form2.add(label);
            form2.getToolbar().setBackCommand("Back", Toolbar.BackCommandPolicy.ALWAYS, ll -> {
                count.set(count.get() + 1);
                numberLabel.setText(count.get() + "");
                startScreen.showBack();
            });
    
            button1.addActionListener(l -> {
                form2.show();
            });
    

    在我看来,是否重新创建 Form 的实例应该根据具体情况进行评估。在考虑之间的变量中,根据我的谦虚的看法,还有代码的可读性和它的作用,尤其是在复杂的情况下。

    【讨论】:

    • 非常感谢您发布此答案。该解决方案效果很好。我只需要几天的时间来测试一下。在测试过程中,我发现了一个简单的解决方案,它一直就在我的眼前,但出于某种原因,我还没有看到。将标签添加到构造函数(下面的代码)也可以在运行后退命令时更新文本。
    • 为了简单起见,我只是采用这种方法。否则,我将不得不对现有代码进行太多更改。但是在我未来的一个项目中,你的代码肯定会派上用场。它简单、优雅、高效。我以前没有使用过 Wrapper 类。这也是一个有趣的方法。还有:Toolbar.BackCommandPolicy.ALWAYS 是做什么的?
    • 感谢您的 cmets。 Toolbar.BackCommandPolicy 有六个选项在这里解释:codenameone.com/javadoc/com/codename1/ui/…。具体来说,ALWAYS 选项始终在工具栏中显示后退命令。对于 iOS 版本,这是必不可少的,但例如在 Android 上,您可能对仅使用硬件后退按钮而不在工具栏中插入文本或图标感兴趣:因此,有六种不同的选项可用。模拟器的某些 Android 皮肤允许您模拟按下硬件后退按钮。
    【解决方案2】:

    重新创建表单实例的开销可以忽略不计,因此这不是问题,但近年来我们尝试更多地重用表单实例。不是因为性能。

    好处在于轻微的行为,例如在表单中滚动位置。这些很难复制。

    【讨论】:

    • 嗨 Shai,感谢您的快速回复。我不能完全按照你的解释:你的意思是做类似 form.revalidate() 而不是调用类吗?
    • 表单重新验证不会重新加载数据,因此这不是一个真正的选择。您需要对表单实现某种对象绑定。通常,您希望在返回时拥有“相同的形式”/
    【解决方案3】:

    在测试过程中,我找到了一个简单的解决方案,就是将标签添加到构造函数中。我希望这个 sn-p 能有所帮助。

    public ClassTwo(Form a, ClassPojo classPojo, Label label) { 
    
        Command back = new Command("A") {
            @Override
            public void actionPerformed(ActionEvent evt) {
    
            label.setText(classPojo.getStringTest());
            a.showBack(); 
         }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多