簡體   English   中英

Java TextField日期自動填充

[英]Java TextField Date auto fill

好吧,我得到了這樣的東西:

public void menu() {

        final Form menu = new Form("Menu");
        menu.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Button confirm = new Button("Confirm");

        Container creditCardContainer = new Container(new GridLayout(1, 3));

        final TextField num1 = new TextField(3);
        final TextField num2 = new TextField(3);
        final TextField num3 = new TextField(3);

        num1.setConstraint(TextArea.NUMERIC);
        num2.setConstraint(TextArea.NUMERIC);
        num3.setConstraint(TextArea.NUMERIC);

        creditCardContainer.addComponent(num1);
        creditCardContainer.addComponent(num2);
        creditCardContainer.addComponent(num3);

        Validator v = new Validator();

        v.addConstraint(num1, new LengthConstraint(2));
        v.addConstraint(num2, new LengthConstraint(2));
        v.addConstraint(num3, new LengthConstraint(4));

         automoveToNext(num1, num2);
         automoveToNext(num2, num3);

        menu.add(creditCardContainer);
        menu.add(confirm);
        v.addSubmitButtons(confirm);
        menu.show();

            confirm.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent ev) 
                {
                    String getdate = num1.getText() + "/" + num2.getText() + "/" + num3.getText();
                    System.out.println(getdate);
                    new StateMachine("/theme");
                }
            });
        }
    }

    private void automoveToNext(final TextField current, final TextField next) {
        current.addDataChangedListener(new DataChangedListener() {
            public void dataChanged(int type, int index) {
                if(current.getText().length() == 3) {
                    Display.getInstance().stopEditing(current);
                    String val = current.getText();
                    current.setText(val.substring(0, 2));
                    next.setText(val.substring(2));
                    Display.getInstance().editString(next, 3, current.getConstraint(), next.getText());
                }
            }
        });
    }

請注意,不建議使用addDataChangeListener ,因此我不得不將其更改為addDataChangedListener

我認為我的代碼有問題,因為當我在Codename One Simulator中運行它時,即使使用以下代碼,它仍然允許我鍵入字母:

num1.setConstraint(TextArea.NUMERIC);
num2.setConstraint(TextArea.NUMERIC);
num3.setConstraint(TextArea.NUMERIC);

另外,當我完成日期輸入后,我的確認按鈕也不會像應有的那樣突出顯示。 請有人幫我修復它。

Obs:我的約會日期應該是dd / MM / yyyy

運用

try {
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Date date = format.parse("");
}catch (ParseException e) {
    System.out.println("Wrong format.");
}

檢查日期是否為有效格式。

我們不支持直接字段屏蔽,因為本機文本字段輸入處理得不太好。 您可以想到2種選擇:

  • 使用“日期選擇器”啟動一個很棒的設備本機UI來選擇日期。 注意,它在模擬器中並不是很好,但是在Android / iOS上看起來會不錯。

  • 使用3個文本字段,然后像鍵入此信用卡輸入示例一樣自動將其移至下一個: http : //www.codenameone.com/blog/validation-regex-masking.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM