GWT中数字文本字段的解决方案

时间:2010-05-19 12:57:57

标签: gwt gwt2

我需要一个与Gxt的NumberField非常相似的文本字段。不幸的是,我没有在我的应用程序中使用Gxt,而GWT 2.0还没有实现数字文本字段。

因此,目前通过使用keyboardHandler过滤掉非数字键击,我可以选择模拟NumberField。

这是解决问题的最佳方法吗?这里有没有人有更好的解决方案/方法?

提前致谢:)

6 个答案:

答案 0 :(得分:15)

在这里,您可以找到我在其中一个课程中使用的代码。这些功能比GXT的功能更加有限,但应该让你走上正轨。

这是一个非常基本的小部件,但是做了我需要的。

public class ValueTextBox extends TextBox {

    private int min = 0;
    private int max = 100;
    private boolean minConstrained = true;
    private boolean maxConstrained = true;
    private int minDigits = 1;
    private int step = 1;

    private KeyUpHandler keyUpHandler = new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent event) {
            if (isReadOnly() || !isEnabled()) {
                return;
            }

            int keyCode = event.getNativeEvent().getKeyCode();

            boolean processed = false;

            switch (keyCode) {
            case KeyCodes.KEY_LEFT:
            case KeyCodes.KEY_RIGHT:
            case KeyCodes.KEY_BACKSPACE:
            case KeyCodes.KEY_DELETE:
            case KeyCodes.KEY_TAB:
                if (getText().isEmpty()) {
                    setValue(formatValue(min));
                }
                return;
            case KeyCodes.KEY_UP:
                if (step != 0) {
                    increaseValue();
                    processed = true;
                }
                break;
            case KeyCodes.KEY_DOWN:
                if (step != 0) {
                    decreaseValue();
                    processed = true;
                }
                break;
            }

            if (processed) {
                cancelKey();
            }
        }

    };

    private KeyPressHandler keyPressHandler = new KeyPressHandler() {
        @Override
        public void onKeyPress(KeyPressEvent event) {

            if (isReadOnly() || !isEnabled()) {
                return;
            }

            int keyCode = event.getNativeEvent().getKeyCode();

            switch (keyCode) {
            case KeyCodes.KEY_LEFT:
            case KeyCodes.KEY_RIGHT:
            case KeyCodes.KEY_BACKSPACE:
            case KeyCodes.KEY_DELETE:
            case KeyCodes.KEY_TAB:
            case KeyCodes.KEY_UP:
            case KeyCodes.KEY_DOWN:
                return;
            }

            int index = getCursorPos();
            String previousText = getText();
            String newText;
            if (getSelectionLength() > 0) {
                newText = previousText.substring(0, getCursorPos())
                        + event.getCharCode()
                        + previousText.substring(getCursorPos()
                                + getSelectionLength(), previousText.length());
            } else {
                newText = previousText.substring(0, index)
                        + event.getCharCode()
                        + previousText.substring(index, previousText.length());
            }
            cancelKey();

            setValue(newText, true);
        }
    };

    public ValueTextBox(int value) {
        this(value, 0, 100);
    }

    public ValueTextBox(int value, int min, int max) {
        this(value, min, max, true);
    }

    public ValueTextBox(int value, int min, int max, boolean constrained) {
        this(value, min, max, constrained, constrained);
    }

    public ValueTextBox(int value, int min, int max, boolean minConstrained,
            boolean maxConstrained) {
        super();

        addKeyPressHandler(keyPressHandler);
        addKeyUpHandler(keyUpHandler);

        this.min = min;
        this.max = max;
        this.minConstrained = minConstrained;
        this.maxConstrained = maxConstrained;

        setValue(formatValue(value), false);
        setTextAlignment(TextBoxBase.ALIGN_CENTER);
        setStyleName(Resources.INSTANCE.css().fwFormEntry());
    }

    public void setMinDigits(int minDigits) {
        if (minDigits > 0) {
            this.minDigits = minDigits;

            String value = getText();
            long newValue = parseValue(value);

            setText(formatValue(newValue));
        }
    }

    public void setSteps(int step) {
        this.step = step;
    }

    protected void increaseValue() {
        if (step != 0) {
            String value = getText();
            long newValue = parseValue(value);
            newValue += step;
            if (maxConstrained && (newValue > max)) {
                return;
            }
            setValue(formatValue(newValue));
        }
    }

    protected void decreaseValue() {
        if (step != 0) {
            String value = getText();
            long newValue = parseValue(value);
            newValue -= step;
            if (minConstrained && (newValue < min)) {
                return;
            }
            setValue(formatValue(newValue));
        }
    }

    /**
     * @param value
     *            the value to format
     * @return the formatted value
     */
    protected String formatValue(long value) {
        String newValue = String.valueOf(value);

        if (minDigits > newValue.length()) {
            String leading = StringUtils.repeat("0", (minDigits - newValue
                    .length()));
            newValue = leading + newValue;
        }

        return newValue;
    }

    @Override
    public void setValue(String value) {
        setValue(value, false);
    }

    @Override
    public void setValue(String value, boolean fireEvents) {
        try {
            long newValue = parseValue(value);
            if ((maxConstrained && (newValue > max))
                    || (minConstrained && (newValue < min))) {
                return;
            }
            String prevText = getValue();
            super.setText(formatValue(newValue));
            if (fireEvents) {
                ValueChangeEvent.fireIfNotEqual(this, getValue(), prevText);
            }
        } catch (Exception ex) {
            // Do Nothing
            System.out.println(ex.getMessage());
        }
    }

    /**
     * @param value
     *            the value to parse
     * @return the parsed value
     */
    protected long parseValue(String value) {
        return Long.valueOf(value);
    }
}

更新 :代码位于https://github.com/ctasada/GWT-Eureka

答案 1 :(得分:6)

这是一个简单的KeyPressHandler,允许用户输入十进制数字;

public void onKeyPress(KeyPressEvent event){
    TextBox sender = (TextBox)event.getSource();

    if (sender.isReadOnly() || !sender.isEnabled()) {
        return;
    }

    Character charCode = event.getCharCode();
    int unicodeCharCode = event.getUnicodeCharCode();

    // allow digits, '.' and non-characters
    if (!(Character.isDigit(charCode) || charCode == '.' || unicodeCharCode == 0)){
        sender.cancelKey();
    }
}

答案 2 :(得分:3)

不知道这些类何时被添加到GWT,但它们对我来说没有任何额外的代码可以正常工作:

com.google.gwt.user.client.ui.DoubleBox
com.google.gwt.user.client.ui.IntegerBox
com.google.gwt.user.client.ui.LongBox 

对于更高级的验证,您可能希望使用一些自定义Parser覆盖其基类ValueBox ...

答案 3 :(得分:1)

这是我对NumberField的实现。功能与Carlos的版本非常相似,但还支持十进制输入和非数字键过滤。

public class NumberBox extends TextBox
{
private boolean isDecimal = false;

public NumberBox( )
{
}

public boolean isDecimal( )
{
    return isDecimal;
}

public void setDecimal( boolean isDecimal )
{
    this.isDecimal = isDecimal;
}

public Integer getIntegerValue( )
{
    return ( StringUtil.isEmpty( getSanitizedValue( ) ) ) ? null : Integer.parseInt( getSanitizedValue( ) );
}

@Override
protected void initialize( )
{
    super.initialize( );
    addStyleName( "number" );

    this.addKeyPressHandler( new KeyPressHandler( )
    {
        public void onKeyPress( KeyPressEvent event )
        {
            if ( !isEnabled( ) || isReadOnly( ) )
                return;

            int keyCode = event.getNativeEvent( ).getKeyCode( );

            // allow special keys
            if ( ( keyCode == KeyCodes.KEY_BACKSPACE )
                    || ( keyCode == KeyCodes.KEY_DELETE )
                    || ( keyCode == KeyCodes.KEY_ENTER ) || ( keyCode == KeyCodes.KEY_ESCAPE ) || ( keyCode == KeyCodes.KEY_RIGHT )
                    || ( keyCode == KeyCodes.KEY_LEFT ) || ( keyCode == KeyCodes.KEY_TAB ) )
                return;

            // check for decimal '.'
            if ( isDecimal( ) && '.' == (char)keyCode && !getValue( ).contains( "." ) )
                return;

            // filter out non-digits
            if ( Character.isDigit( charCode ) )
                return;

            cancelKey( );
        }
    } );
}

}


PS:Superclass TextBox是一个自定义类,它扩展了GWT TextBox以及一些额外的应用程序特定功能。方法initialize()基本上是在TextBox构造函数中调用的,而getSanitizedValue通过修剪进行一些基本的健全性检查。

答案 4 :(得分:1)

Carlos Tasada 回答有效,但包含一个错误:你应该在switch / case块之前添加event.isShiftKeyDown()检查onKeyPress处理程序。它将传递一些符号,如'('否则。

答案 5 :(得分:1)

根据Julian Downes的回答,您可以这样做:

text.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            TextBox sender = (TextBox) event.getSource();

            if (sender.isReadOnly() || !sender.isEnabled()) {
                return;
            }
            if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
                return;
            }

            Character charCode = event.getCharCode();

            try{
                Double.parseDouble(sender.getText().concat(charCode.toString()));
            }
            catch(Exception e){
                sender.cancelKey();
            }
        }
    });