set text(number) to Edit Field through its setchangelistener

↘锁芯ラ 提交于 2019-12-12 02:43:21

问题


Hi I want to accept number in edit field up to two decimal.So I am setting listener to it Then I am checking whether number is two decimal or more and if it is more than two decimal then i am truncating the number and again trying to set the truncated number.But it showing 104 error at this place interestRate.setText(text).My code is

interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text=interestRate.getText().toString();
code here--
interestRate.setText(text);
}
};

So my question is whether text can be set from its listener or not


回答1:


Looks like you just need to use some condition check in order not to get in an infinite loop:

interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
    public void fieldChanged(Field field, int context) {
        String text = interestRate.getText().toString();
        // code here to create a truncated text
        if (!truncated.equals(text)) {
            // next time we will not get here 
            // because truncated will be equal to text
            interestRate.setText(text);
        }
    }
});


来源:https://stackoverflow.com/questions/7570570/set-textnumber-to-edit-field-through-its-setchangelistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!