开发人员指南中的automoveToNext()方法无法正常工作

时间:2018-04-09 23:09:32

标签: android ios codenameone

我正在使用开发者指南中的automoveToNext方法获取电话号码字段。即使在模拟器上,我也能够重现这个问题。在第四个字段中输入数字后,它将回到第一个字段或第二个字段,并开始覆盖第一个字段,第二个字段等等。请查看附带的屏幕截图以供参考。输入的文本也会闪烁。请告诉我,如果任何人有任何不同的文本字段控制用于具有moveToNextField功能的电话号码。此外,该领域的焦点不是适当切换:意思是,光标将出现多个字段。请找到以下代码。

第二个问题,我是否可以将相同的字段用于带扩展名组合的电话号码。 Codenameone没有电话号码控制区域代码和扩展名分割。请让我知道

菲尔兹声明:

TextField num1 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num2 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num3 = new TextField("", "1234", 4, TextArea.NUMERIC);
TextField num4 = new TextField("", "1234", 4, TextArea.NUMERIC);

方法:

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


private void automoveToNext2(final TextField current, final TextField next) {
    current.addDataChangeListener(new DataChangedListener() {
        public void dataChanged(int type, int index) {                
            if (current.getText().length() == 5) {
                Display.getInstance().stopEditing(current);
                String val = current.getText();
                current.setText(val.substring(0, 4)); 
                next.setText(val.substring(5));
                Display.getInstance().editString(next, 4, current.getConstraint(), next.getText());

            }
        }
    });
}

private void automoveToNext3(final TextField current, final TextField num1, final TextField num2, final TextField num3) {
    current.addDataChangeListener(new DataChangedListener() {
        public void dataChanged(int type, int index) {
            if (current.getText().length() == 5) {
                current.stopEditing();
                String val = current.getText();
                current.setText(val.substring(0, 4));
                current.setFocus(true);
                if (num1.getText().length() == 3) {
                    num1.stopEditing();
                }
                if (num2.getText().length() == 3) {
                    num2.stopEditing();
                }
                if (num3.getText().length() == 4) {
                    num3.stopEditing();
                }
                if (current.getText().length() == 4) {
                    current.stopEditing();
                }

            }
        }
    });
}

调用这样的方法:

        if (num2.getText().isEmpty()) {
            automoveToNext1(num1, num2);
        }
        if (num3.getText().isEmpty()) {
            automoveToNext1(num2, num3);
        }
        if (num4.getText().isEmpty()) {
            automoveToNext2(num3, num4);
            automoveToNext3(num4, num1, num2, num3);
        }

1 个答案:

答案 0 :(得分:1)

有一个带控件的cn1lib可以让你选择区域:https://www.codenameone.com/blog/tip-activation-ui-builder-pattern.html

Uber clone app的登录页面中有类似的控件。

自动移动到下一个的代码似乎有点旧尝试:

private void onTypeNext(TextField current, TextField next, int length) {
    current.addDataChangedListener((i, ii) -> {
        if(current.getText().length() == length) {
            current.stopEditing();
            next.startEditingAsync();
        }
    });
}