【问题标题】:BlackBerry - Set the text width of a EditField from a changeListener eventBlackBerry - 从 changeListener 事件设置 EditField 的文本宽度
【发布时间】:2012-09-18 21:19:38
【问题描述】:

如果input.getText() 返回的长度大于13,则用户输入的最后一个字符不应出现在编辑字段中。如果第 13 个字符是“,”,则程序应允许在“,”之后增加 2 个字符。这样,编辑字段的最大长度为 16。

像这样限制 EditField 的文本宽度的选项是什么?

input = new BorderedEditField();

input.setChangeListener(new FieldChangeListener() {             
    public void fieldChanged(Field field, int context) {
        if(input.getText().length() < 13)
            input.setText(pruebaTexto(input.getText()));
        else
            //do not add the new character to the EditField
    }
});

public static String pruebaTexto(String r){
    return r+"0";
}

【问题讨论】:

  • 稍微解释一下你的问题。
  • 如果 input.getText() 返回的长度大于 13,则用户输入的最后一个字符不应出现在编辑字段中。清楚了吗?
  • 好的,如果我理解正确,那么您需要一个最多可以包含 13 个字符的 EditField,是吗?如果是,那么您可以使用setMaxSize(int size)。这也可以在使用public EditField(String label, String initialValue, int maxNumChars, long style) 构造 EditField 实例时完成。查看 API 文档以获取更多信息,blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/…blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/…
  • 好吧,我没有提到它,但是如果在最后一个字符(13)之后,用户输入',',程序应该允许在','之后再输入2个字符。这样,编辑字段的最大长度将为 16。无论如何,我稍后会尝试您的解决方案。谢谢!
  • 您错过了问题中最复杂的部分。在这种情况下,限制最大字符数对您没有帮助。您可以覆盖keyChar 或类似方法来控制每个按键事件,api 链接blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/…。祝你好运:)。

标签: blackberry java-me


【解决方案1】:

我编写了一个简单的BorderedEditField 类,它扩展了EditField。此类的方法protected boolean keyChar(char key, int status, int time) 被修改,以便可以操作EditField 的默认行为。如果您发现此示例有帮助,那么您可以改进实现。

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.MainScreen;

public final class MyScreen extends MainScreen {
    public MyScreen() {
        BorderedEditField ef = new BorderedEditField();
        ef.setLabel("Label: ");

        add(ef);
    }
}

class BorderedEditField extends EditField {
    private static final int MAX_LENGTH = 13;
    private static final int MAX_LENGTH_EXCEPTION = 16;

    private static final char SPECIAL_CHAR = ',';

    protected boolean keyChar(char key, int status, int time) {
        // Need to add more rules here according to your need.
        if (key == Characters.DELETE || key == Characters.BACKSPACE) {
            return super.keyChar(key, status, time);
        }
        int curTextLength = getText().length();
        if (curTextLength < MAX_LENGTH) {
            return super.keyChar(key, status, time);
        }
        if (curTextLength == MAX_LENGTH) {
            char spChar = getText().charAt(MAX_LENGTH - 1);
            return (spChar == SPECIAL_CHAR) ? super.keyChar(key, status, time) : false;
        }
        if (curTextLength > MAX_LENGTH && curTextLength < MAX_LENGTH_EXCEPTION) {
            return super.keyChar(key, status, time);
        } else {
            return false;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多