开发者

How to filter keys in MIDP TextField?

开发者 https://www.devze.com 2022-12-20 17:26 出处:网络
I have a TextField.PhoneNumber but I would like to filter out \"+\" character. In other words, I need a new constraint for TextField. Is there a way to define a new constraint with TextField?

I have a TextField.PhoneNumber but I would like to filter out "+" character. In other words, I need a new constraint for TextField. Is there a way to define a new constraint with TextField?

How about开发者_运维知识库 preventing keys from cycling on a mobile phone within a midp?


It might not what you really want. But, MIDP does not support change constraint rule as you want. So, I suggest HACK for your purpose.

How about use ItemStateListener to check if text field contains string which you want to filter out and if this string is exist, change text field forcefully.

The code could be looks like below:

// set item state listener
form.setItemStateListener(this);

// check if text field contains invalid string
// then replace it
public void itemStateChanged(Item item) {
    if (item == getTextField()) {
        TextField t = (TextField)item;
        String s = t.getString();
        // + is invalid string
        int pos = s.indexOf("+");
        if (pos != -1) {
            t.setString(s.substring(0, pos) + s.substring(pos + 1));
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消