2011-09-13 48 views
0

我做一個自定義對話框喜歡爲:如何在此自定義對話框上設置按鈕動作?

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); 

    } 

我如何設置「視圖按鈕」和「取消按鈕」 行動? 我搜查了但沒有找到我要做的事。 請幫幫我!

回答

0

看看這個代碼..這可能幫助你..

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); 
    } 
} 
+0

非常感謝你,這段代碼非常有用。但我使用動作關閉()爲「取消按鈕」,我必須點擊** 2次**關閉對話框。我在模擬器上工作。你能解釋爲什麼嗎? –

+0

哦,這是模擬器的問題,我改變了simualotor,它運作良好。 :) –

1

附上DialogClosedListenerCustomDialog使用Dialog.setDialogClosedListener()來。當有人點擊其中一個按鈕時,將調用DialogClosedListener.dialogClosed()方法,按鈕索引將作爲參數choice傳遞。

+0

是的,非常感謝 –

+0

如果您提供了一個Object對象數組,但是如果您使用自定義按鈕字段,則返回的'choice'始終爲-1。 – azdev

相關問題