【问题标题】:How to set action for button on this custom dialog?如何在此自定义对话框上设置按钮的操作?
【发布时间】:2011-09-13 20:50:13
【问题描述】:

我制作了一个自定义对话框,如下所示:

public class CustomDialog extends Dialog {
     public CustomDialog(String s) {
    super(s, new String[] {"View","Cancel"}, new int [] {1,2}, 1,         Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.FOCUSABLE);

    }

如何为“查看按钮”和“取消按钮”设置动作? 我搜索并没有找到我必须做的事情。 请帮帮我!

【问题讨论】:

    标签: blackberry customdialog


    【解决方案1】:

    使用Dialog.setDialogClosedListener()DialogClosedListener 附加到您的CustomDialog。当有人点击任一按钮时,将调用DialogClosedListener.dialogClosed() 方法,并将按钮索引作为choice 参数传递。

    【讨论】:

    • 如果您提供一个 Object 数组选项,则此方法有效,但如果您使用自定义按钮字段,则返回的 choice 始终为 -1。
    【解决方案2】:

    查看此代码..这可能会对您有所帮助..

    import net.rim.device.api.ui.component.ButtonField;
    import net.rim.device.api.ui.component.Dialog;
    import net.rim.device.api.ui.container.HorizontalFieldManager;
    
    public class CustomAlertDialog extends Dialog {
    
    
        public CustomAlertDialog() {
            super("Your Custom message for Dialoug" , null, null, Dialog.DISCARD, null, Dialog.VERTICAL_SCROLL);
    
            HorizontalFieldManager hfm = new HorizontalFieldManager();
    
            ButtonField view = null;
    
            view = new ButtonField("view") {
                protected boolean navigationClick(int status, int time) {
                // do what ever you want
                return true;
                }
    
                protected boolean keyChar(char key, int status, int time) {
                // do what ever you want
                return true;
                }
            };
    
            ButtonField cancel = null;
            cancel = new ButtonField("Cancel") {
                protected boolean navigationClick(int status, int time) {
                // do what ever you want
                return true;
                }
    
                protected boolean keyChar(char key, int status, int time) {
                // do what ever you want
                return true;
                }
            };
        hfm.add(view);
        hfm.add(cancel);
    
        this.add(hfm);
        }
    }
    

    【讨论】:

    • 非常感谢,这段代码很有用。但是我对 "Cancel Button" 使用 action close() ,我必须单击 2 次​​b> 才能关闭对话框。我在模拟器上工作。你能解释一下为什么吗?
    • 哦,是模拟器的问题,我换了模拟器,效果很好。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多