0

によって返される長さ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";
}
4

1 に答える 1

1

BorderedEditFieldを拡張する単純なクラスをコーディングしましたEditFieldprotected 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;
        }
    }
}
于 2012-09-18T13:19:38.997 に答える