黑莓 - 从ButtonField消费点击事件

时间:2010-05-14 05:15:57

标签: user-interface events blackberry button

我在黑莓中开发了UI应用程序 按钮点击事件我想显示添加两个数字的结果。
如何在Blackberry的Java应用程序中处理此事件?

2 个答案:

答案 0 :(得分:5)

ButtonField中使用点击事件的最佳选择是使用FieldChangeListener。只需实现FieldChangeListener接口并应用ButtonField。setChangeListener()

class MenuScreen extends MainScreen implements FieldChangeListener {
    EditField mNumberAEditField;
    EditField mNumberBEditField;
    ButtonField mAddButtonField;
    public MenuScreen() {
        super(DEFAULT_CLOSE);
        mNumberAEditField = new EditField("A:", "2", 10, 
                EditField.FILTER_INTEGER);
        add(mNumberAEditField);
        mNumberBEditField = new EditField("B:", "2", 10, 
                EditField.FILTER_INTEGER);
        add(mNumberBEditField);
        mAddButtonField = new ButtonField("A + B", 
                ButtonField.CONSUME_CLICK);
        add(mAddButtonField);
        mAddButtonField.setChangeListener(this);
    }

    public void fieldChanged(Field field, int context) {
        if(field == mAddButtonField)
        {
            showAddResult();
        }
    }

    private void showAddResult() {
        String message = "";
        String aStr = mNumberAEditField.getText();
        String bStr = mNumberBEditField.getText();
        if(aStr == "")
            message = "Enter A value!";
        else if(bStr == "")
            message = "Enter B value!";
        else    
        {
            int res = Integer.parseInt(aStr)+Integer.parseInt(bStr);
            message = aStr+" + "+bStr+" = "+String.valueOf(res);        
        }
        Dialog.inform(message);
    }
}

FieldChangeListener的其他选项是内联声明和实现它。你不必检查来自哪个现场活动:

        mAddButtonField.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                showAddResult();
            }
        });

答案 1 :(得分:2)

或..

add(new ButtonField("Click Me")) {  
  protected boolean navigationClick(int status, int time) {  
    showAddResult();  
    return true;  
  }  
});