gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:30:37 26 4
gpt4 key购买 nike

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

因此,目前我可以选择通过使用键盘处理程序过滤掉非数字击键来模拟 NumberField。

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

提前致谢:)

最佳答案

在这里您可以找到我在我的一门类(class)中使用的代码。这些功能比 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

关于gwt - GWT 中数字文本字段的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2865647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com