如何在Blackberry中制作按钮来执行某些操作

如何在Blackberry中制作按钮来执行某些操作,blackberry,java-me,Blackberry,Java Me,我试图制作一个按钮,当它被按下时显示一条消息,但无法让它工作 这是我的按钮: HorizontalFieldManager buttonManager = new HorizontalFieldManager(ButtonField.FIELD_HCENTER); messageButton = new ButtonField("Press me", ButtonField.CONSUME_CLICK); messageButton.setChangeListener(this); b

我试图制作一个按钮,当它被按下时显示一条消息,但无法让它工作

这是我的按钮:

HorizontalFieldManager buttonManager =
    new HorizontalFieldManager(ButtonField.FIELD_HCENTER);

messageButton = new ButtonField("Press me", ButtonField.CONSUME_CLICK);
messageButton.setChangeListener(this);
buttonManager.add(messageButton);
add(buttonManager);
下面是打印消息的方法:

public void fieldChanged(Field field, int context) {
    if (field == messageButton) {
        showMessage();
    } 
}

private void showMessage() {
    Dialog.inform("The button was pressed");
}
我是否在showMessage()方法中出错,或者在其他地方识别错误?

检查事件日志(Alt+LGLG),我怀疑您会看到在非事件线程上推送模式屏幕的错误。如果是这种情况,只需将
showMessage
更改为

private void showMessage() 
{
   UiApplication.getUiApplication.invokeLater(new Runnable()
   {
      public void run()
      {
         Dialog.inform("The button was pressed");
      }
   });
}

我认为问题可能是您传递给构造函数的标志。如果你想用按钮做些什么,这肯定不是你想用的标志

除此之外,我建议不要对字段使用
FieldChangeListener
。可能对于按钮是可以的,但是对于其他组件,
fieldChanged
方法可以在布局期间在没有用户交互的情况下调用。这就是为什么您几乎总是希望过滤掉第二个参数(代码中的上下文)等于的调用


更好的替代方法是覆盖
导航单击
,这也适用于触觉屏幕和常规屏幕。

调用器仍然很有用,因为fieldChanged在UI引擎操作链中的某个点被调用,您最好让它在显示对话框之前完成任务。我怀疑不使用invokeLater是问题的主要原因

// just paste this class and run 







package mypackage;

import java.io.IOException;

import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;


public final class MyScreen extends MainScreen
{

    public MyScreen()
    {        
        // Set the displayed title of the screen       


        HorizontalFieldManager horizontalFieldManager= new HorizontalFieldManager();
        ButtonField buttonField= new ButtonField("click to show dialog",Field.FIELD_VCENTER);
        horizontalFieldManager.add(buttonField);
        buttonField.setChangeListener(buttonchangelisners);
        add(horizontalFieldManager);

    }

    private FieldChangeListener buttonchangelisners= new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            // TODO Auto-generated method stub

            showDialog("show your message");
        }
    };

     public void showDialog(final String message) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert(message);
            }
        });
    }
}

使用fieldChangeListener时,实际上需要使用单击,否则将调用上下文菜单。如果切换到在按钮字段内使用navigationClick,则可以返回true以实现相同的行为(尽管我更喜欢使用navigationUnclick)。

fieldChanged(..)在事件线程上运行,因此这不是问题所在。我建议将对话框放在If语句之外,以确保调用fieldChanged()方法。请确保您也只有1个fieldChanged()方法。我已经测试了这段代码,它工作正常。你的问题在别处。我想这是对我对另一个答案的评论的回应。我的观点是,另一个答案是错误的,所建议的改变实际上并不能解决问题。至于在显示对话框之前是否应该让Ui交互完成,这取决于您的环境。